Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Randomly shuffle an array


'assumes MyArray already exists and is filled with data
'if you know the upper/lower limits of the array, just use them
'set temp variable to same type as MyArray
Dim i As Long , j As Long , temp As Variant
Randomize
For i = Lbound (MyArray) To Ubound (MyArray)
    'randomly pick an element of the array to swap with element i
   j = Int(( Ubound (MyArray)) * Rnd + 1)
   temp = MyArray(i)
   MyArray(i) = MyArray(j)
   MyArray(j) = temp
Next i