in reply to Per cents and Probabilities
#!/usr/bin/perl -w use strict; my %probs = ( 'fifty' => 50, 'ten' => 10, 'forty' => 40, ); # generate lookup for the above my @lookup; foreach my $key (keys(%probs)) { for (1..$probs{$key}) { push @lookup, $key; } } # now pick one: sub pickOne { $lookup[ int(rand($#lookup)) ]; } # test it my %bins; for (1..1000) { $bins{ pickOne() }++; } for my $key (keys(%bins)) { print $key, "\t", $bins{$key}/10, "%\n"; }
|
|---|