2014-02-19 27 views
0

使用Node.js,我們試圖想出一種生成1到100之間的隨機數的方法。但是,不使用標準線性像典型的RAND()函數那樣的分佈可能我們想要改爲使用威布爾(或某種這樣的)分佈來給出一個長尾,並且將更多的答案權衡給更大的值 - 例如可能產生一個75到100的值80%的時間,15%的時間會產生50到74的值,其餘的(< 20)會產生5%的時間。根據使用Node.js的特定分佈生成1到100之間的隨機變量

我們使用以下公式 alpha *( - ln(1-X))^(1/beta)找到了一個Weibul隨機變量函數。假設X是從0到1的標準線性隨機數,使用alpha = 1,並且beta似乎給我們一個很好的分佈,但是我們難以知道如何生成一個總是在1和1之間的單值100.

任何意見或建議表示讚賞。

+1

威布爾分佈定義從0到無窮大。你覺得你如何適應[1..100]? – alex

+0

爲什麼不寫一些函數輸出一系列隨機值(如75-100),並使用另一個隨機數來選擇要使用的範圍。如果你最稀有的百分比是5%,那麼產生一個從1到20的數字; 1-16 =大,17 = <20,18 + 19 + 20 = 50-74等... – dandavis

回答

2

好的 - 以防萬一任何人對我的答案版本感興趣。使用下面的參考了我對我的約束威布爾結果集的目標:

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

以下鏈接也是得心應手:

http://en.wikipedia.org/wiki/Weibull_distribution

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(); 
}); 

我已經運行了不同的'形狀'值的代碼,它提供給我完全我需要的結果。