[Date Prev][Date Next][Thread Prev][Thread Next]
[Search]
[Date Index]
[Thread Index]
Re: [FWP] Probabilistic selection
Paul Makepeace wrote:
> I implemented this fairly straightforwardly as follows,
> my $sum = 0; $sum += $_ for values %target;
> ...
sub random_link {
my $target_hr = shift;
# create the deck:
my @a = map { ($_) x $target_hr->{$_} } keys %$target_hr;
# shuffle it:
for my $i ( reverse 1 .. $#a ) { # F-Y
my $j = rand $i;
@a[$i,$j] = @a[$j,$i];
}
# turn up the top card:
shift @a
}
--
John Porter
==== Want to unsubscribe from Fun With Perl? Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
==== unsubscribe