使用Node.js,我们试图想出一种产生介于1到100之间的随机数的方法。然而,我们可能不使用像典型的RAND()函数那样的标准线性分布,而是使用Weibull (或某些类似的)分布,它将给出一个长尾,并将答案更重地加权到更大的值--例如,80%的时间可能生成75到100的值,15%的时间生成50到74的值,其余的(< 20)生成时间的5%。
我们用下面的公式α*(-ln(1-X))^(1/β)找到了一个随机变量函数。假设X是从0到1的标准线性随机,使用alpha = 1,而β<= 1似乎给我们一个很好的分布,但是我们很难生成一个总是介于1到100之间的单一值。
如有任何意见或建议,将不胜感激。
发布于 2014-02-21 07:45:57
好吧-万一有人对我的答案感兴趣。使用以下引用使我达到了约束Weibull结果集的目标:
以下链接也很方便:
http://mathworld.wolfram.com/WeibullDistribution.html
最后,js代码看上去如下所示:
var desiredMin = 1; //the desired minimum random result
var desiredMax = 50; //the desired maximum random result
var scale = 1; //Weibul scale parameter (1 provides an inverse result set)
var shape = 10; //Weibul shape parameter (how skewed the result set is)
//given an possible random range of {0,1} value calculate the minimum and maximum Weibull distribution values given current shape and scale parameters
var weibullMin = Math.pow((shape*scale), (-1*shape)) * Math.pow(.000001, shape -1) * Math.exp(Math.pow((.000001/scale), shape));
var weibullMax = Math.pow((shape*scale), (-1*shape)) * Math.pow(.999999, shape -1) * Math.exp(Math.pow((.999999/scale), shape));
//simulate 1000 random weibull values and write results to file
fileStream.once('open', function(fd) {
for(var i = 1; i <= 1000; i++) {
var r = Math.random();
var weibullRandom = Math.pow((shape*scale), (-1*shape)) * Math.pow(r, shape-1) * Math.exp(Math.pow((r/scale), shape)); //generate Weibull random value
var weibullRandomAdjusted = desiredMin + (desiredMax - desiredMin)*((weibullRandom-weibullMin) / (weibullMax - weibullMin)); //adjust Weibull random value so that it constrained into the desired min/max range
fileStream.write(weibullRandomAdjusted + "\n");
};
fileStream.end();
});
我已经运行了改变形状值的代码,它提供了我所需要的结果。
https://stackoverflow.com/questions/21892553
复制相似问题