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

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.