Why linear search? hehehe, snicker :)
#!/usr/bin/perl # http://perlmonks.org/?node_id=1127420 use Search::Dict; use warnings; use strict; my $max = 0; my $file = join '', map s/(\S*)$/ $max += $1 /er, <DATA>; open my $fh, '<', \$file or die "open failure $!"; my $picker = sub { look $fh, rand $max, {comp => sub {shift =~ s/.* //r <=> shift}}; (split ' ', <$fh>)[0]; }; my %tally; # now test it $tally{$picker->()}++ for 1 .. 1e4; use YAML; print Dump \%tally; __DATA__ A 1e-7 B 20e-7 C 10e-5
In reply to Re^12: an algorithm to randomly pick items that are present at different frequencies
by Anonymous Monk
in thread an algorithm to randomly pick items that are present at different frequencies
by efoss
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |