首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用Node.js根据特定的分布在1到100之间生成随机变量

使用Node.js根据特定的分布在1到100之间生成随机变量
EN

Stack Overflow用户
提问于 2014-02-19 13:14:26
回答 1查看 1.3K关注 0票数 0

使用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之间的单一值。

如有任何意见或建议,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-21 07:45:57

好吧-万一有人对我的答案感兴趣。使用以下引用使我达到了约束Weibull结果集的目标:

https://stats.stackexchange.com/questions/30303/how-to-simulate-data-that-satisfy-specific-constraints-such-as-having-specific-m

以下链接也很方便:

distribution

http://mathworld.wolfram.com/WeibullDistribution.html

最后,js代码看上去如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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();
});

我已经运行了改变形状值的代码,它提供了我所需要的结果。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21892553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文