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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |