Apr 10, 2011

i am trying to find out how it shuffles the number 1, 2, 3 and 4 without repeating the same number within the series. So far, I think that "i" begins as 1 and RN is a randomized number between 1 and 4. The do while loop checks if "i" is the same as RN and spits out the flag as true or false. Then "i" is increased 1 at a time and so does the number of elements in the array. I believe that the array a(4) and the for loop does something that "remembers" the same numerical value not to be repeated. I'm not sure exactly what the for loop does and how the flag make this thing work.

Lets say, a case where i=1 and the RN=3, the flag becomes false. Therefore, a(1)=3. Then, i increments by 1 and a(j) becomes a(2), which puts out the values 1 and 2. Since both numbers are flag=false, it increments again. Because a(i) = RN, a(2) becomes = 3. How can this be? It has a(1) equal to 3 and a(2) also equal to 3. Each array is supposed to have NON-OVERLAPPING numbers.

The result of doing shuffle() is a random set of numbers that don't overlap:

1234, 1243, 1342, 1324, 2341, 3412, 4321, 3214, and so on.

but never 1111, 1112, 3333 or 3242.

To find out how exactly it works, I tried to delete some lines, tweak the numbers. The result of that is either numbers change to repeats, all zeros or a logical error. The following is the code placed in the module and can be used for other forms and in this case it is used for the labels in form1 windows form:

Sub Shuffle()

Dim a(4), i, j, RN As Integer

Dim flag As Boolean

[code]....

View 2 Replies
View Related