|XP is just a number|
Weighted random numbers generatorby spurperl (Priest)
|on Mar 13, 2003 at 16:40 UTC||Need Help??|
spurperl has asked for the wisdom of the Perl Monks concerning the following question:
* This is more a call-for-code-feedback than a question...
* Please mentally perform s/random/pseudo-random/ on this post
A weighted random generator is needed when events should happen at random, but some should have a higher probability of happening than others ("All animals are equal, but pigs are more equal than the others" ... Orwell). For example, I'd like to decide between 0, 1 and 2 randomly, with 0 and 1 having an equal chance to be chosen, and 2 having a chance that is twice higher.
The following code shows a function implementing this weighted random generator, with some test code. Please tell me what do you think about it... Any obvious slumps ? What about efficiency ?
Thanks in advance