Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4,VB5,VB6,VBS
Expertise: beginner
Nov 20, 1999

ArrayShuffle - Randomize the order of elements in an array

' Shuffle the elements of an array of any type
' (it doesn't work with arrays of objects or UDT)

Sub ArrayShuffle(arr As Variant)
    Dim index As Long
    Dim newIndex As Long
    Dim firstIndex As Long
    Dim itemCount As Long
    Dim tmpValue As Variant
    
    firstIndex = LBound(arr)
    itemCount = UBound(arr) - LBound(arr) + 1
    
    For index = UBound(arr) To LBound(arr) + 1 Step -1
        ' evaluate a random index from LBound to INDEX
        newIndex = firstIndex + Int(Rnd * itemCount)
        ' swap the two items
        tmpValue = arr(index)
        arr(index) = arr(newIndex)
        arr(newIndex) = tmpValue
        ' prepare for next iteration
        itemCount = itemCount - 1
    Next

End Sub
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date