Shuffle

Sean Kelly sean at f4.ca
Mon Jan 28 07:46:49 PST 2008


Bill Baxter wrote:
> Kris wrote:
>> I hope tango.core.Array.shuffle got it right?
>>
>>> Wrong again. The calculationof "max" should be inside the loop, since
>>> it depends on "n" which is different for each iteration. 
> 
> It looks like Tango's shuffle doesn't attempt to eliminate the modulo
> bias in generating a random number.  So it doesn't have attempt to
> calculate any "n" or "max" to begin with.

The Tango shuffle uses the Knuth-Fischer-Yates shuffle algorithm but
iterates from 0 .. N rather than N .. 0.  It may be that reversing the
direction causes some bias and that it really has to be top-down
however--I'm planning on reading up on the algorithm today to be sure.
I actually stumbled on the current implementation by accident simply by
following the requirements for random_shuffle in the C++ spec.


Sean


More information about the Digitalmars-d-announce mailing list