Maybe this explains it better?

The following program uses this heavily biased PRNG:

sub badRand($) { rand() < 0.5 ? 0 : rand( $_[0] ) }

which is designed to produce 0, 50% of the time. That means that when asked for numbers 0-9, it produces '0', 10 times more often than any of the other values:

[549637, 50008, 50261, 50195, 50365, 49871, 50000, 49692, 49768, 50203 +]

And when asked for 0..23, produces '0' 25 times more often than any other number:

[ 521654, 20747, 20716, 20614, 20914, 20740, 20906, 20580, 20625, 2107 +7, 20736, 20912, 20835, 20820, 20958, 20818, 20866, 20818, 20969, 206 +46, 20749, 20953, 20454, 20893, ]

But when that biased PRNG used within a standard Fisher Yates shuffle, the shuffles are fair, no matter how many times you run it:

{ ABCD => 67436, ABDC => 67742, ACBD => 67990, ACDB => 68277, ADBC => 68018, ADCB => 67846, BACD => 67766, BADC => 67859, BCAD => 68598, BCDA => 68919, BDAC => 69172, BDCA => 67870, CABD => 67755, CADB => 67192, CBAD => 67657, CBDA => 67731, CDAB => 67793, CDBA => 67680, DABC => 67755, DACB => 68016, DBAC => 68098, DBCA => 67988, DCAB => 67666, DCBA => 68408, }

The test code:

#! perl -slw use strict; use Data::Dump qw[ pp ]; sub badRand($) { rand() < 0.5 ? 0 : rand( $_[0] ) } sub shuffle { $a = $_ + badRand( @_ - $_ ), $b = $_[$_], $_[$_] = $_[$ +a], $_[$a] = $b for 0 .. $#_; return @_; } my @rands; ++$rands[ badRand( 10 ) ] for 1 .. 1e6; pp \@rands; <STDIN> +; my @vals = ( 'A' .. 'D' ); my %tests; my $c = 0; while( 1 ) { ++$tests{ join '', shuffle( @vals ) }; unless( ++$c % 576 ) { system 'cls'; pp \%tests; } } __END__ c:\test> junk77 [549637, 50008, 50261, 50195, 50365, 49871, 50000, 49692, 49768, 50203 +] { ABCD => 67436, ABDC => 67742, ACBD => 67990, ACDB => 68277, ADBC => 68018, ADCB => 67846, BACD => 67766, BADC => 67859, BCAD => 68598, BCDA => 68919, BDAC => 69172, BDCA => 67870, CABD => 67755, CADB => 67192, CBAD => 67657, CBDA => 67731, CDAB => 67793, CDBA => 67680, DABC => 67755, DACB => 68016, DBAC => 68098, DBCA => 67988, DCAB => 67666, DCBA => 68408, }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

In reply to Re^2: Shuffling CODONS by BrowserUk
in thread Shuffling CODONS by WouterVG

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.