The algorithm needs a minor tweak:
#!/usr/bin/perl # http://perlmonks.org/?node_id=1173445 use strict; use warnings; use List::Util qw(sum); my @count = (0) x 20; for (1 .. 2e4) { my ($top, $bottom) = (20, 20 * 20); my @orderedRands = grep{ $bottom > 0 && rand(1) < $top / $bottom ? (--$top, --$bottom, 1) : (--$bottom, 0) } map{ ($_) x 20 } 1 .. 20 ; $count[$_]++ for grep {defined} @orderedRands; } my $sum = sum(@count); print "$_\t=>\t$count[$_]\t(", sprintf('%.2f', ($count[$_]/$sum)*100), + "% TTL)\n" for 1 .. 20;
...which produces...
1 => 20031 (5.01% TTL) 2 => 20037 (5.01% TTL) 3 => 19831 (4.96% TTL) 4 => 19953 (4.99% TTL) 5 => 19840 (4.96% TTL) 6 => 20068 (5.02% TTL) 7 => 19868 (4.97% TTL) 8 => 20030 (5.01% TTL) 9 => 19790 (4.95% TTL) 10 => 20076 (5.02% TTL) 11 => 19944 (4.99% TTL) 12 => 19909 (4.98% TTL) 13 => 20049 (5.01% TTL) 14 => 20150 (5.04% TTL) 15 => 19940 (4.98% TTL) 16 => 20112 (5.03% TTL) 17 => 19973 (4.99% TTL) 18 => 20157 (5.04% TTL) 19 => 20088 (5.02% TTL) 20 => 20154 (5.04% TTL)
Which looks pretty good.
In reply to Re^3: random #s
by tybalt89
in thread random #s
by cboPerl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |