in reply to Shuffle an array
Basically the method works like this.
N is the number of items in the array.
Pick an element from 1 to N at random. Switch that element with the first element.
Now pick an element between 2 and N at random. Switch that element with the second element.
Then pick an element beween 3 and N at random. And switch that element with the third element.
Repeat those two step N-1 times and you have shuffled the array.
If that doesn't explain it for you, try getting a pack of card, put them in order and do the process manually.Even using cards 1 to 6 and a dice, and doing the process a couple of times should clarify things if my words don't help.
In Perl, the counting runs 0 to N-1 each time, but the logic remains the same.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Shuffle an array
by FireBird34 (Pilgrim) on Nov 09, 2002 at 02:06 UTC | |
|
Re: Re: Shuffle an array
by thinker (Parson) on Nov 09, 2002 at 10:54 UTC | |
by BrowserUk (Patriarch) on Nov 09, 2002 at 11:14 UTC |