in reply to an algorithm to randomly pick items that are present at different frequencies
I'm going to assume you want something to get picked each time, so you want their probabilities to reflect their relative frequency values. In that case, in pseudo-code:
loop through the lines parse out the key and the frequency save the key and frequency as a two-element array inside an array keep a running total of the frequencies set an accumulator to 0 loop through the array replace each frequency value with a value representing the frequency plus the accumulator, divided by the total of the frequencies add the original frequency to the accumulator get a random positive number less than 1 loop through the array one more time if the random number is less than the relative frequency value we've found our key, print it and exit the loop
If that makes sense to you, try that and ask if you need help, showing the code you have so far.
Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: an algorithm to randomly pick items that are present at different frequencies
by efoss (Acolyte) on May 27, 2015 at 05:15 UTC |