in reply to Randomly choosing from a set of alternatives with varying popularity
I'm sure that there is a better way to do this, but just off the top of my head:
use List::Util qw/ shuffle max /; my %animals = ( alligator => 100, bear => 90, cat => 80, jellyfish => +10 ); my @popular_list = shuffle map { ( $_ ) x $animals{ $_ } } keys %anima +ls; my $popular_pick = $popular_list[ rand @popular_list ]; my @impartial_list = shuffle keys %animals; my $impartial_pick = $impartial_list[ rand @impartial_list ]; my $max_val = 1 + max values %animals; my @unpopular_list = shuffle map { ( $_ ) x ( $max_val - $animals{ $_ +} ) } keys %animals; my $unpopular_pick = $unpopular_list[ rand @unpopular_list ];
|
|---|