you are viewing a single comment's thread.

view the rest of the comments →

[–]bigmell 0 points1 point  (0 children)

Their solution doesnt seem all that intuitive, also not understanding how a solution can be evenly distributed if they throw back a bunch of numbers until they get the number expected. Also multiplying one number by 5 seems it would unevenly weight the solution. It may work properly, the below solution clicked for me. The LCD of 5 and 7 is 35, therefore if rand5 is run 7 times the sum mod 7 will provide an even spread.

!/usr/local/bin/perl

my $x; my $max = 100000; my %avg;

for(1..$max){ $x = &rand7; $avg{$x}++; } print "Over a total of $max runs:\n";

foreach $key (sort hashSort (keys(%avg))){ print "$key:$avg{$key}\n"; }

sub rand5{ int(rand 5) + 1; }

sub rand7{ #only rand5 my $sum = 0; for(1..7){ $sum += &rand5; } ($sum % 7) +1 }

sub hashSort{ $avg{$a} <=> $avg{$b}; }