概率分布的随机数发生器 [英] Distributed probability random number generator
问题描述
欲生成基于分布式概率的数字。例如,只说有每个数字下面OCCURENCES:
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?
我很高兴这是基于一个静态的,硬$ C $光盘一套,但现在我最终还是希望它得到数据库查询的概率分布。
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?
推荐答案
的一般方法是从0..1区间饲料均匀分布的随机数到<一个href=\"http://en.wikipedia.org/wiki/Cumulative_distribution_function#Inverse_distribution_function_.28quantile_function.29\">the你所需的分布的累积分布函数的倒数。
The general approach is to feed uniformly distributed random numbers from 0..1 interval into the inverse of the cumulative distribution function of your desired distribution.
因此,在你的情况下,只需使用一个随机数x,从0..1(例如使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.random.nextdouble.aspx\"><$c$c>Random.NextDouble()$c$c>)并根据其价值回归
Thus in your case, just draw a random number x from 0..1 (for example with Random.NextDouble()
) and based on its value return
- 1,如果0℃= X&LT;二百零八分之一百五十零,
- 2,如果二百零八分之一百五十零&LT; = X&LT;二百零八分之一百九十零,
- 3,如果二百零八分之一百九十〇&LT; = X&LT;二百零八分之二百零五和
- 4,否则。
这篇关于概率分布的随机数发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!