The code for your algorithm is easy:

my @random_numbers; push(@random_numbers, int(rand(54)) + 1) foreach 1..10000; my %counts; $counts{$_}++ foreach @random_numbers; printf("%2d: %d\n", @$_) foreach sort { $a->[1] <=> $b[1] || $a->[0] <=> $b[0] } map { [ $_, $counts{$_} ] } keys(%counts);
He generated 10000 random numbers between 1 and 54 and stored the frequency distribution in his array.

Can the same number be picked twice? If not, your algo isn't representative. here's a fixedup version:

# Picks 6 unique numbers uniformly between 1 and 54 inclusive. sub pick6 { my @nums; push(@nums, int(rand(54 - $_)) + 1) foreach 0..5; for my $i (1..5) { for my $j (0..$i-1) { $num[$i]++ if $num[$i] >= $num[$j]; } } return @nums; } my @random_numbers; push(@random_numbers, pick6()) foreach 1..10000; my %counts; $counts{$_}++ foreach @random_numbers; printf("%2d: %d\n", @$_) foreach sort { $a->[1] <=> $b[1] || $a->[0] <=> $b[0] } map { [ $_, $counts{$_} ] } keys(%counts);
Hint: imbedded FOR LOOPS

"embedded" is spelled with an "e", and "nested" is the proper term when talking about loops within loops.


In reply to Re: Messing Around by ikegami
in thread Messing Around by Perl12

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.