16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javascriptで正規分布

Posted at

平均付近に集中するような値の分布を「正規分布」といいます。
「ガウス分布」ともいいます。
Javascriptには一様分布させるためのMath.randomというメソッドは存在していますが、正規分布用のメソッドは用意されていません。

そこで ボックス=ミュラー法と呼ばれるアルゴリズムを用います。

一様分布に従う確率変数から標準ガウス分布に従う確率変数を生成させる手法。

だそうです。

sample code

// mean 平均
// sd 標準僅差
function normalDistribution(sd,mean){
	var x = Math.random();
	var y = Math.random();
	
	var z1 = Math.sqrt(-2*Math.log(x))*Math.cos(2 * Math.PI  * y);
	var z2 = Math.sqrt(-2*Math.log(x))*Math.sin(2 * Math.PI  * y);

	return {z1:sd+z1*mean,z2:sd+z2*mean};
}


16
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?