If I want to randomly generate a dice (4 side) roll, I can use the following expression:
$roll = 1 + int( rand(4));
This assumes that every side of this dice has equal chance of being rolled (0.25). Thats easy. Now What if I say, that the probability of rolling a 3 is 70% and the probability of rolling a 1 or 2 or 4 is 10%.
i.e. $probability = { '1' => 0.1, '2' => 0.1, '3' => 0.7, '4' => 0.1 }
You can use the "pick a random line" algorithm from the FAQ:
sub roll { my $die = shift;
my $result; my $total = 0;
while (my ($face, $weight) = each %$die) {
$result = $face if rand($total += $weight) < $weight;
}
return $result; }
-- Steve
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>