Skip to content

Instantly share code, notes, and snippets.

@sifue
Last active April 29, 2016 01:16
Show Gist options
  • Save sifue/3632692 to your computer and use it in GitHub Desktop.
Save sifue/3632692 to your computer and use it in GitHub Desktop.
正規分布(標準ガウス分布)のランダム関数 - ボックス=ミューラー法 ref: http://qiita.com/sifue/items/e1dbfe671f42886e47d6
/**
* 正規分布乱数関数 参考:http://d.hatena.ne.jp/iroiro123/20111210/1323515616
* @param number m 平均μ
* @param number s 分散σ^2
* @return number ランダムに生成された値
*/
var normRand = function (m, s) {
var a = 1 - Math.random();
var b = 1 - Math.random();
var c = Math.sqrt(-2 * Math.log(a));
if(0.5 - Math.random() > 0) {
return c * Math.sin(Math.PI * 2 * b) * s + m;
}else{
return c * Math.cos(Math.PI * 2 * b) * s + m;
}
};
for (var i = 0; i < 100; i++) {
console.log(normRand(80, 5));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment