<分区>
Possible Duplicate:
Generate random number with non-uniform density
我尝试识别/创建一个函数(在 Java 中),它给我一个非均匀分布的数字序列。
如果我有一个函数说 function f(x), and x>0
它会给我一个随机数
从 0
到 x
。
该函数最适用于任何给定的 x
,下面这只是我想要的示例。
但是如果我们说 x=100
函数 f(x)
将返回 s 非均匀分布。
我想举例来说
0 到 20
大约占所有案例的 20%。
21 到 50
大约占所有案例的 50%。
51 到 70
大约占所有案例的 20%。
71 到 100
大约是所有情况下的 10。
简而言之,它给了我一个像正态分布一样的数字,在这种情况下,它是 30-40,x
是 100
。
http://en.wikipedia.org/wiki/Normal_distribution
(如果需要,我可以使用一个统一的随机生成作为分数,并且只有一个函数可以将统一结果转换为非统一结果。)
编辑
我对这个问题的最终解决方案是:
/**
* Return a value from [0,1] and mean as 0.3, It give 10% of it is lower
* then 0.1. 5% is higher then 0.8 and 30% is in rang 0.25 to 0.45
*
* @return
*/
public double nextMyGaussian() {
double d = -1000;
while (d < -1.5) {
// RANDOMis Java's normal Random() class.
// The nextGaussian is normal give a value from -5 to +5?
d = RANDOM.nextGaussian() * 1.5;
}
if (d > 3.5d) {
return 1;
}
return ((d + 1.5) / 5);
}