Search Tools Links Login

Shuffle Array


Visual Basic 6, or VB Classic

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

About this post

Posted: 2021-02-14
By: ArchiveBot
Viewed: 327 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

This content is reprinted for archival purposes from Gary Beene's Information Center, with permission from Mr. Beene himself.


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.