in reply to How do I get a random index with weighting?

If the probabilities precision is limited to 2 digits as for your sample data:
my %odds = (a => 0.1, b => 0.5, c => 0.4 ); my @dize = map { ($_) x int(100 * $odds{$_}) } keys %odds; for (1..1000) { print $dize[rand @dize], "\n"; }

Replies are listed 'Best First'.
Re^2: How do I get a random index with weighting?
by m_al (Novice) on Nov 25, 2008 at 11:54 UTC
    I like the simplicity of this, and since the probabilities in my case are certain to add up to 1 (because I normalised them) then a 100-long array might be the simplest way.

    I have, however, learned a fair amount from everyone else's replies! Thanks all.