Skip to content

Instantly share code, notes, and snippets.

@arodland
Created July 15, 2009 02:07
Show Gist options
  • Save arodland/147389 to your computer and use it in GitHub Desktop.
Save arodland/147389 to your computer and use it in GitHub Desktop.
my $total = 0;
while (($id, $weight) = $sth->fetchrow) {
$total += $weight;
push @choices, [$total, $id];
}
my $rand = rand($total);
my $chosen = undef;
for my $choice (@choices) {
if ($choice->[0] >= $rand) {
$chosen = $choice->[1];
last;
}
}
if (!defined $chosen) {
warn "Can't happen";
}
$word = word($chosen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment