# shuffle sub

• ### Question

• hi everyone, can someone explain how this code shuffles?  this snippet comes from developer's guide to small basic.

For i=1 To 25
Array[i]=i
EndFor

For j=25 To 2 Step -1
i=math.GetRandomNumber(j)
temp = Array[j]
Array[j]=array[i]
Array[i]=temp
EndFor

Thursday, June 19, 2014 10:30 PM

• The only reason try do it with 5 playing cards is it is easier to see what is happening than with 25!

i=math.GetRandomNumber(j)

This will set i to be a random number between 1 and j (1, 2, 3,4...j).  It is possible that i will be set to j and the card will be swapped with itself (no change).  But apart from that i will be the current card at a random position <= to card j, which may be a card that was previously swapped or not.

EDIT

Another way to visualize it is:

1. Randomly select a card from a pile of cards and place it in a second pile.
2. Repeat [1] while you still have cards in the first pile.
3. When the last card is placed on the second pile, you are done.

To make the logic exactly like the code we could replace 1 with:

1. Take the bottom card from a pile of cards, then randomly swap it with another card in the pile (including itself as a random choice), then place the swapped card in a second pile.

Saturday, June 21, 2014 8:03 AM

### All replies

• Basically it randomly swaps array elements.

It loops through each element from the last (25th) to the 2nd :  For j=25 To 2 Step -1

selects a random card to swap with (i) less than or equal to j : i=math.GetRandomNumber(j)

does the swap:

• store element j  in temp : temp = Array[j]
• put element i in element j : Array[j]=array[i]
• put the stored element that was in j in i : Array[i]=temp

So we select each card from the end in turn, and swap it with a random card selected from one of the cards not selected yet.

Try it with a pack of cards, following the algorithm, try with just 5 cards first.

Thursday, June 19, 2014 10:41 PM
• assume there are 5 values, i'm still confused by this statement

i=math.GetRandomNumber(j)

how does it know to pick a value not selected from the previous selection?  since it is a random number generator, can't it just select the same number from the previous run?  am i missing something obvious?  thank you for helping.

Friday, June 20, 2014 11:55 PM
• The only reason try do it with 5 playing cards is it is easier to see what is happening than with 25!

i=math.GetRandomNumber(j)

This will set i to be a random number between 1 and j (1, 2, 3,4...j).  It is possible that i will be set to j and the card will be swapped with itself (no change).  But apart from that i will be the current card at a random position <= to card j, which may be a card that was previously swapped or not.

EDIT

Another way to visualize it is:

1. Randomly select a card from a pile of cards and place it in a second pile.
2. Repeat [1] while you still have cards in the first pile.
3. When the last card is placed on the second pile, you are done.

To make the logic exactly like the code we could replace 1 with:

1. Take the bottom card from a pile of cards, then randomly swap it with another card in the pile (including itself as a random choice), then place the swapped card in a second pile.

Saturday, June 21, 2014 8:03 AM
• thank you!  i see it now
Saturday, June 21, 2014 3:21 PM
• This question reminded me of a  graphics simulation of a lottery program I wrote more then a year ago. I just published it as RMV445.

It draws 20 numbers in random sequence. The numbers are unique, because if an already drawn number shows, a new one it drawn. The startscreen shows the possible adjustments.

Jan [ WhTurner ] The Netherlands

Sunday, June 22, 2014 10:49 AM