CodeItBetter Programming Another VB Programming Blog

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
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.