|
|
Title | Randomize a list |
Description | |
Keywords | randomize, shuffle |
Categories | Algorithms, Tips and Tricks |
|
|
This program the unsorting algorithm described in my book Ready-to-Run Visual Basic Algorithms.
For each item, the algorithm randomly selects an item at that position or later in the array and swaps the two. This produces a randomized list. See my book for a proof.
|
|
' Randomize the list.
Public Sub RandomizeList(items() As Integer)
Dim min_item As Integer
Dim max_item As Integer
Dim i As Integer
Dim j As Integer
Dim tmp_value As Integer
min_item = LBound(items)
max_item = UBound(items)
For i = min_item To max_item - 1
' Randomly assign item number i.
j = Int((max_item - i + 1) * Rnd + i)
tmp_value = items(i)
items(i) = items(j)
items(j) = tmp_value
Next i
End Sub
|
|
For more information on this and many other useful algorithms, see my book Ready-to-Run Visual Basic Algorithms.
|
|
|
|
|
|