随机数,非均匀分布 [英] Random number,with nonuniform distributed
问题描述
可能重复:
生成密度不均匀的随机数
<我尝试识别/创建一个函数(在Java中),它给出了一个非均匀分布的数字序列。
如果我的函数说函数f(x),而x> 0
它将从<$ c $给我一个随机数
c> 0 到 x
。
该功能最适用于任何给出 x
,以下只是我想要的一个例子。
The function most work with any given x
and this below is only a example how I want to have.
但是如果我们说 x = 100
函数 f(x)
将返回s nonunifrom分发。
But if we say x=100
the function f(x)
will return s nonunifrom distributed.
我希望例如说
0到20
约为20%案例。
21到50
约为所有案例的50%。
21 to 50
be approximately 50% of all case.
51到70
大约占所有情况的20%。
51 to 70
be approximately 20% of all case.
71到100
大约是所有情况中的10个。
71 to 100
be approximately 10 of all case.
简而言之,给我一个正常分布的数字,它偷看30-在这种情况下,40美元 x
是 100
。
In short somting that give me a number like normal distribution and it peek at 30-40 in this case x
is 100
.
< a href =http://en.wikipedia.org/wiki/Normal_distribution =nofollow noreferr呃> http://en.wikipedia.org/wiki/Normal_distribution
(如果需要,我可以使用统一的随机数作为分数,仅限将统一结果转换为非均匀结果的函数。 )
( I can use a uniform random gen as score if need, and only a function that will transfrom the uniform result to a non-uniform result. )
编辑
我对此问题的最终解决方案是:
My final solution for this problem is:
/**
* 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);
}
推荐答案
你能写一个函数吗?它将1-X范围内的一些随机数加起来并取平均值?当n增加时,这将趋于正态分布
Could you just write a function that sums a number of random numbers it the 1-X range and takes an average? this will tend to the normal distribution as n increases
参见:
我攻击了以下内容:
class CrudeDistribution {
final int TRIALS = 20;
public int getAverageFromDistribution(int upperLimit) {
return getAverageOfRandomTrials(TRIALS, upperLimit);
}
private int getAverageOfRandomTrials(int trials, int upperLimit) {
double d = 0.0;
for (int i=0; i<trials; i++) {
d +=getRandom(upperLimit);
}
return (int) (d /= trials);
}
private int getRandom(int upperLimit) {
return (int) (Math.random()*upperLimit)+1;
}
}
Commons-Math中的库可以生成分布基于均值和标准差(衡量价差)。并在链接中执行此操作的一些算法。
There are libraries in Commons-Math that can generate distributions based on means and standard deviations (that measure the spread). and in the link some algorithms that do this.
可能是寻找相关2班轮的有趣时段:
Probably a fun hour of so of hunting to find the relevant 2 liner:
https://commons.apache.org/math/userguide/distribution .html
这篇关于随机数,非均匀分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!