Perl Monk, Perl Meditation | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
spurperl,
I have only briefly read the other replies, but I believe my solution may be unique. If it isn't, then I apologize. First let me solve your overly simplistic example, then give you the logic behind a more general solution: 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.
This can be reduced to generating a random number between 1 and 4 and then checking the results.
The more general approach to this is to calculate how many elements are required to cover the entire range, and then set up the range logic to check the results. I haven't provided any code because it is pretty straight forward, but if you would like to see some code that can generate the weighting dynamically - let me know as it less straight forward (but still not too difficult). Hope this helps - L~R Update: Just realized this is my 100th post - woo hoo In reply to Re: Weighted random numbers generator
by Limbic~Region
|
|