in reply to Re: Fisher-Yates shuffle?
in thread Fisher-Yates shuffle?
It didn't take me long to realise (after I had seen your analysis) that rand(n), when int'd, gives me values 0..(n-1). I should have spotted that earlier.
It took me a lot longer to understand why it required a changed from rand($_-1) to rand($_+1) rather rand($_) to effect the desired results.
Maybe others spotted this right away, but incase anyone who reads this doesn't get it, I'll give my explanation and risk correction.
I had thought that what was required for the shuffle to work was to swap the last element with a random choice of the preceeding elements, then the second from last with its preceeding elements and so on.
It took me while to realise that you have to allow the last element to also have the possibility of 'swapping with itself' otherwise you exclude 1/n of the possible outcomes. Omitting this possibility for all of the passes (in your testcase) gives 3! possible outcomes rather than 4!.
But that led me to wondering why my original code only resulted in 2 outcomes!
It took me a while longer to see this resulted from the combination of the int'd rand and the loop starting at 1 not zero. Meaning that whilst n-1 swaps are performed, 2 of these swaps are always the same. Ie. no randomness involved.
So, one swap with a choice of two, and 2 swaps with no choice = 2 possible outcomes.
All that said, the most significants thing to come out of this (for me) are
Thankyou for both.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Fisher-Yates shuffle?
by Abigail-II (Bishop) on Aug 12, 2002 at 09:29 UTC |