Home » Arrays, Collections, Lists » How to Scramble the order of elements in an array.
How to Scramble the order of elements in an array.
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 'Arrays, Collections, Lists - How to Scramble the order of elements in an array. Public Sub ShuffleArray(ByRef vArray As Variant, Optional startIndex As Variant, _ Optional endIndex As Variant) Dim I As Long Dim rndIndex As Long Dim Temp As Variant If IsMissing(startIndex) Then startIndex = LBound(vArray) End If If IsMissing(endIndex) Then endIndex = UBound(vArray) End If For I = startIndex To endIndex rndIndex = Int((endIndex - startIndex + 1) * Rnd() + startIndex) Temp = vArray(I) vArray(I) = vArray(rndIndex) vArray(rndIndex) = Temp Next I End Sub |
Enjoy this article?
Filed under: Arrays, Collections, Lists
Leave a comment