Shuffle

Walter Bright newshound1 at digitalmars.com
Fri Jan 25 11:51:04 PST 2008


Bill Baxter wrote:
> Yep, this is a classic problem from homeworks sets for algorithms 
> classes.  How to make a random permutation of N elements in O(N) time 
> and O(1) additional space such that every element is equally likely to 
> end up in any slot.  And the answer is what Oskar said.    Walter, your 
> MechE background is showing. :-)

I remember reading a criticism of the shuffle/swap algorithm and the fix 
a while back, but don't remember where I read it or what the fix was. 
I'm glad Oskar has set it straight here.

Although this is just a dumb program, because it's going into a 
'cookbook' that others may use for more serious applications, the 
algorithms should be correct.

Kudos to Oskar.


More information about the Digitalmars-d-announce mailing list