的范围具有不同的概率内产生随机数 [英] generate random numbers within a range with different probabilities

查看:129
本文介绍了的范围具有不同的概率内产生随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能生成一个随机数A之间= 1,B = 10,每个数字都有不同的概率有多大?

How can i generate a random number between A = 1 and B = 10 where each number has a different probability?

例如:号码/概率

1 - 20%

2 - 20%

3 - 10%

4 - 5%

5 - 5%

...等等。

我所知道的一些硬codeD的解决方法,不幸的是较大范围的没有用的,例如A = 1000,B = 100000

I'm aware of some hard-coded workarounds which unfortunately are of no use with larger ranges, for example A = 1000 and B = 100000.

假设我们有一个

    Rand()

方法,它返回一个随机数R,0℃下R< 1,任何人都可以发布一个code样品有这样做的正确方法? prefferable在C#/爪哇/动作。

method which returns a random number R, 0 < R < 1, can anyone post a code sample with a proper way of doing this ? prefferable in c# / java / actionscript.

推荐答案

有一个优雅的算法归因于克努特为AJ·沃克(电子快报10,8(1974年),127-128; ACM跨数学软件3(1977年),253-256)。

There is an elegant algorithm attributed by Knuth to A. J. Walker (Electronics Letters 10, 8 (1974), 127-128; ACM Trans. Math Software 3 (1977), 253-256).

的想法是,如果你有合计k * n个n个不同颜色的球,那么有可能分发球在正的容器,使得容器中没有。我包含的颜色我和至多一个其他颜色的球。证明是通过归纳n个。用于感应步骤挑具有最少数量的球的颜色

The idea is that if you have a total of k * n balls of n different colors, then it is possible to distribute the balls in n containers such that container no. i contains balls of color i and at most one other color. The proof is by induction on n. For the induction step pick the color with the least number of balls.

在您的例子每组10乘用合适的米,使得它们的所有整数的概率。所以,也许M = 100,你有20个球的颜色0,20球的颜色1,10球的颜色2,5球3色的,等等。所以,K = 10。

In your example n = 10. Multiply the probabilities with a suitable m such that they are all integers. So, maybe m = 100 and you have 20 balls of color 0, 20 balls of color 1, 10 balls of color 2, 5 balls of color 3, etc. So, k = 10.

现在产生n维的表,每个项是一个概率和其它颜色(颜色我相较于其他颜色的球的比值)。

Now generate a table of dimension n with each entry being a probability (the ration of balls of color i vs the other color) and the other color.

要生成一个随机的球,产生在区间[0随机浮点数R,N)。让我成为整数部分(R地板)和X过剩(R - 我)。

To generate a random ball, generate a random floating-point number r in the range [0, n). Let i be the integer part (floor of r) and x the excess (r – i).

if (x < table[i].probability) output i
else output table[i].other

该算法的优点在于,对于每个随机球你只使一个单一的比较

The algorithm has the advantage that for each random ball you only make a single comparison.

让我工作了一个例子(同克努特)。

Let me work out an example (same as Knuth).

考虑模拟扔一对骰子。

因此​​P(2)= 1/36,P(3)= 2/36,P(4)= 3/36,P(5)= 4/36,P(6)= 5/36,P (7)= 6/36,P(8)= 5/36,P(9)= 4/36,P(10)= 3/36,P(11)= 2/36,P(12)= 1 / 36。

So P(2) = 1/36, P(3) = 2/36, P(4) = 3/36, P(5) = 4/36, P(6) = 5/36, P(7) = 6/36, P(8) = 5/36, P(9) = 4/36, P(10) = 3/36, P(11) = 2/36, P(12) = 1/36.

乘以36 * 11获得393球,颜色2的11,颜色3的22,颜色4的33,...,12色11。 我们有K =一十一分之三百九十三= 36。

Multiply by 36 * 11 to get 393 balls, 11 of color 2, 22 of color 3, 33 of color 4, …, 11 of color 12. We have k = 393 / 11 = 36.

表[2] =(11/36,彩色4)

Table[2] = (11/36, color 4)

表[12] =(11/36,彩色10)

Table[12] = (11/36, color 10)

表[3] =(22/36,彩色5)

Table[3] = (22/36, color 5)

表[11] =(22/36,彩色5)

Table[11] = (22/36, color 5)

表[4] =(三十六分之八,彩色9)

Table[4] = (8/36, color 9)

表[10] =(三十六分之八,颜色6)

Table[10] = (8/36, color 6)

表[5] =(16/36,颜色6)

Table[5] = (16/36, color 6)

表[9] =(16/36,彩色8)

Table[9] = (16/36, color 8)

表[6] =(7/36,彩色8)

Table[6] = (7/36, color 8)

表[8] =(6/36,颜色7)

Table[8] = (6/36, color 7)

表[7] =(36/36,颜色7)

Table[7] = (36/36, color 7)

这篇关于的范围具有不同的概率内产生随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆