Live data from Hacker News

Unbiased Randomization with the Fisher-Yates Shuffle

spin.atomicobject.com

1–10 of 14 posts

Re: Unbiased Randomization with the Fisher-Yates Shuffle

#4

If you are using Python, you should use the standard 'random.shuffle' which does use the Fisher-Yates algorithm.

There's value in knowing how it's implemented.

Further, there is value in knowing what makes the incorrect ways incorrect. :)

Also, even in the article it noted to use a library if your language allows.

Re: Unbiased Randomization with the Fisher-Yates Shuffle

#5

If you are using Python, you should use the standard 'random.shuffle' which does use the Fisher-Yates algorithm.

It does seem strange to say "However, there are plenty of languages that don’t provide built in support for such a function, leaving you on your own." and then show examples in Python.

Re: Unbiased Randomization with the Fisher-Yates Shuffle

#10
On second read, I realized that OP didn't even prove Fisher-Yates. I blogged about this several weeks back, and here is an excerpt from it. Some of you might find it useful.

"This is easy to verify for N = 2: You are just flipping a coin to decide if you swap 1 with 2. For N > 2, you just need to show that each of 1 through N has an equal chance of getting the k-th slot for 1 through N. In the first step, every number has a 1/N probability of getting into the first slot. For all other slots: the number has (1-1/N) chance of getting/staying there, then by induction, all slots are equally likely, hence (1-1/N)*1/(N-1) = 1/N. This completes the proof."

Post reply on HN