|
|
| Keep It Simple, Stupid | |
| PerlMonks |
Re: Algorithm Helpby TGI (Vicar) |
| on Jan 19, 2010 at 01:50 UTC ( #818089=note: print w/ replies, xml ) | Need Help?? |
|
You might want to edit your post to clarify that weight does not affect the probability of a given selection. Two ideas You could pre-calculate (perhaps write to data files if there are many) all possible items and their strength/weight values. If your list of possible weight values is dense (few skipped values) put an array of items at each weight at a coresponding index in a master array. So:
If the set of weights is sparse, you might use a hash instead. Then if you want to select a random item of weight 3-5, you can do:
Another way, would be:
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||