I want to generate a number based on a distributed probability. For example, just say there are the following occurences of each numbers:
Number| Count
1 | 150
2 | 40
3 | 15
4 | 3
with a total of (150+40+15+3) = 208
then the probability of a 1 is 150/208= 0.72
and the probability of a 2 is 40/208 = 0.192
How do I make a random number generator that returns be numbers based on this probability distribution?
I'm happy for this to be based on a static, hardcoded set for now but I eventually want it to derive the probability distribution from a database query.
I've seen similar examples like this one but they are not very generic. Any suggestions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…