Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a common way to botch the <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle" rel="nofollow noreferrer">Fisher-Yates shuffle algorithm</a>. See <a href="https://stackoverflow.com/questions/5131341/what-distribution-do-you-get-from-this-broken-random-shuffle/5320428">What distribution do you get from this broken random shuffle?</a> for a lively discussion on properties of this implementation.</p> <hr> <p><strong><em>How is this different from Fisher-Yates?</em></strong></p> <p>For Fisher-Yates, at the <code>k</code>th card you must choose a random number between <code>k</code> and <code>52</code>. You are choosing a random number between <code>1</code> and <code>52</code> each time.</p> <hr> <p>The method you describe is similar to that discussed in <a href="https://stackoverflow.com/questions/5131341/what-distribution-do-you-get-from-this-broken-random-shuffle/5320428">What distribution do you get from this broken random shuffle?</a>, but may not be exactly the same. Your implementation is similar to the well studies "Top to Random" shuffle (see <a href="http://journals.cambridge.org/action/displayAbstract;jsessionid=EA72A81A5646929427D99E6A18A65F7F.tomcat1?fromPage=online&amp;aid=1771344" rel="nofollow noreferrer"><em>Analysis of Top To Random Shuffles</em></a> by Diaconis, Fill and Pitman) that will eventually give a fully shuffled deck, though not in "one pass". The Top to Random shuffle is described as following:</p> <blockquote> <p>Choose a random number <code>p</code> from <code>1</code> to <code>52</code>, and swap the top card with the card at position <code>p</code>. Continue until the top card is the card originally in position <code>52</code>, and after this is randomly placed the deck is in random order.</p> </blockquote> <p>This stop condition is known as a "Stopping Time", and takes quite a while to attain. The Fisher-Yates shuffle is far faster.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload