JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
I know a couple of ways to generate weighted random numbers.
If the weights are all integers and there are not too many of them, I can generate a list and randomly select an element from that list. For example, if the weights are
If the weights are not integers, I can generate a cumulative list. For example, if the weights are
Both of these approaches have limitations. It seems to me that some time ago I read about another method that uses a 2-dimensional array that removed most of these limitations. Can anyone provide a link to that method? Or is there another method that will work with large numbers of non-integer weights?
Thanks
If the weights are all integers and there are not too many of them, I can generate a list and randomly select an element from that list. For example, if the weights are
then the list would be 2 1s, 4 2s, 5 3s, 7 4s, and 9 5s:2 4 5 7 9
Then I just generate a random integer on [1,27]. If that number is 17, for example, the random value would be 4.1 1 2 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5
If the weights are not integers, I can generate a cumulative list. For example, if the weights are
The cumulative list would be0.9 0.7 0.5 0.2 0.1
I can generate a random number on [0, 2.4] and then loop through the list until I find the upper limit. For example, if the random number is 1.937562, that would fall between 1.6 and 2.1, so the random number would be 2.0.9 1.6 2.1 2.3 2.4
Both of these approaches have limitations. It seems to me that some time ago I read about another method that uses a 2-dimensional array that removed most of these limitations. Can anyone provide a link to that method? Or is there another method that will work with large numbers of non-integer weights?
Thanks