in reply to Re: Shuffling CODONS
in thread Shuffling CODONS
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, }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Shuffling CODONS
by bliako (Abbot) on Jun 09, 2018 at 22:47 UTC | |
by BrowserUk (Patriarch) on Jun 10, 2018 at 08:48 UTC | |
by bliako (Abbot) on Jun 10, 2018 at 11:41 UTC | |
by BrowserUk (Patriarch) on Jun 10, 2018 at 13:54 UTC | |
by bliako (Abbot) on Jun 10, 2018 at 17:59 UTC | |
| |
by BrowserUk (Patriarch) on Jun 10, 2018 at 12:46 UTC | |
by BrowserUk (Patriarch) on Jun 10, 2018 at 07:39 UTC |