Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This isn't an answer to your question (I'm not actually sure what you're asking), but I will point out that your shuffling algorithm is not random.</p> <p>First, using <code>%</code> to limit <code>rand</code> to a range of values is usually a bad idea. (See <a href="http://c-faq.com/lib/randrange.html" rel="nofollow noreferrer">Q13.16 from the comp.lang.c FAQ</a>.)</p> <p>Second, it looks like you might be trying to implement the <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle" rel="nofollow noreferrer">Fisher-Yates shuffling algorithm</a>, but your implementation is wrong. It's easy to see that your approach of swapping with previously-swapped elements is problematic; consider shuffling a deck of three elements { 0, 1, 2 }:</p> <pre><code>First iteration Second iteration ------------------------------------ { 0, 1, 2 } { 1, 0, 2 } { 0, 1, 2 } { 0, 2, 1 } ------------------------------------ { 1, 0, 2 } { 0, 1, 2 } { 1, 0, 2 } { 1, 2, 0 } ------------------------------------ { 2, 1, 0 } { 1, 2, 0 } { 2, 1, 0 } { 2, 0, 1 } </code></pre> <p>The first column represents the possible states of the deck after the first iteration (where you swap <code>deck[0]</code> with <code>deck[rand() % 3]</code>).</p> <p>The second column represents the possible states of the deck after the second iteration (where you swap <code>deck[1]</code> with <code>deck[rand() % 3]</code>). </p> <p>There are 9 equally probable states---but this is obviously wrong since there should be only 3! = 6 permutations. And indeed, several states are duplicated, which means that they have a higher probability of occurring. (Note that even if I had proceeded to the third iteration, you'd end up with 27 states for only 6 permutations, and since 27 is not divisible by 6, you'd clearly end up with some states occurring more than others.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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