-log(1 - rand)/$weight #### my @matrix = ( [a => 0.1], [b => 0.5], [c => 0.4] ); my $chosen = pick(@matrix); sub pick { my($choice, $threshold); foreach(@_) { my $rand = -log(1 - rand)/$_->[1]; if(not defined $threshold or $rand < $threshold) { $choice = $_->[0]; $threshold = $rand; } } return $choice; } #### use Math::Random::MT qw(rand); #### my %picked; for(1 .. 1E5) { $picked{pick(@matrix)}++; } use Data::Dumper; print Dumper \%picked; #### $VAR1 = { 'c' => 40200, 'a' => 10042, 'b' => 49758 };