in reply to Re: Random Hash Key according to key frequency
in thread Random Hash Key according to key frequency
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Random Hash Key according to key frequency
by adrianh (Chancellor) on Dec 04, 2002 at 18:43 UTC | |
|
Re: Re: Re: Random Hash Key according to key frequency
by jdporter (Paladin) on Dec 04, 2002 at 18:42 UTC |