Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Randomly choosing from a set of alternatives with varying popularity

by jwkrahn (Abbot)
on Mar 28, 2022 at 02:55 UTC ( [id://11142438]=note: print w/replies, xml ) Need Help??


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 ];
  • Comment on Re: Randomly choosing from a set of alternatives with varying popularity
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11142438]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found