'Este exemplo mostra como embaralhar os elementos
'de um array de inteiros
Module Module1
Sub Main()
' cria um array de inteiros
Dim valores() As Integer = {1, 2, 3, 4, 5, 6, 7, _
8, 9, 10}
' exibe os valores do array na ordem original
For Each valor As Integer In valores
Console.Write(valor & " ")
Next
'vamos embaralhar 3 vezes
Embaralhar(valores, 3)
Console.WriteLine()
' exibe os valores do array na ordem embaralhada
For Each valor As Integer In valores
Console.Write(valor & " ")
Next
Console.WriteLine()
Console.WriteLine("Pressione uma tecla para sair...")
Console.ReadKey()
End Sub
Private Sub Embaralhar(ByRef array As Array, ByVal vezes _
As Integer)
Dim rand As New Random(DateTime.Now.Millisecond)
For i As Integer = 1 To vezes
For i2 As Integer = 1 To array.Length
swap(array(rand.Next(0, array.Length)), _
array(rand.Next(0, array.Length)))
Next i2
Next i
End Sub
Private Sub swap(ByRef arg1 As Object, ByRef arg2 As Object)
Dim strTemp As String
strTemp = arg1
arg1 = arg2
arg2 = strTemp
End Sub
End Module