It might be possible to do only one pass through the hash using a technique like finding a random line from a file when the number of lines in the file isn't known.my $total = 0; foreach my $key (keys %hash) { $total += $hash->{$key}; } my $r = int(rand($total)); my $sum = 0; foreach my $key (keys %$hash) { $sum += $hash->{$key}; if ($r < $sum) return $key; }
In reply to Re: Re: Random Hash Key according to key frequency
by iburrell
in thread Random Hash Key according to key frequency
by artist
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |