in reply to Weighted random selection
I'd just use what moritz suggested, but the code you posted can be written much more succinctly:
my %group = ( ad1 => 1, ad2 => 2, ad3 => 3 ); my @rndselect = map { ($_) x $group{$_} } keys %group; $advertinfo->{advert} = $rndselect[rand @rndselect];
Update: I seem to remember that Knuth had something to say about this in The Art of Computer Programming (probably volume 1). I'll have a look for it tomorrow.
|
|---|