sexta-feira, 30 de julho de 2010

Função para deixar uma array de maneira aleatória

//Função para deixar uma array de maneira aleatória
// Chamada: RandomizeArray( seuArray )

Sub RandomizeArray(ArrayIn As Variant)

Dim cnt As Long
Dim RandomIndex As Long
Dim tmp As Variant

'only if an array was passed
If VarType(ArrayIn) >= vbArray Then

'loop through the array elements in reverse
For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1

'select a random array index
RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn))

'cnt represents one array member
'index, and RandomIndex represents
'another, so swap the data held in
'myarray(cnt) with that in myarray(RandomIndex)
tmp = ArrayIn(RandomIndex)
ArrayIn(RandomIndex) = ArrayIn(cnt)
ArrayIn(cnt) = tmp

Next

Else

'The passed argument was not an
'array; error handler goes here

End If

End Sub

Nenhum comentário:

Postar um comentário