java產生常態分佈方法

來源:互聯網
上載者:User

//常態分佈函數,從三種產生法中隨機播放一種,更加隨機
public class Normal {
public Normal() {
}

public double normalRandom1(double a, double b) {//注意這裡的b是方差,等於標準差的平方
double temp = 12;
double x = 0;
for (int i = 0; i < temp; i++)
x = x + (Math.random());
x = (x - temp / 2) / (Math.sqrt(temp / 12));
x = a + x * Math.sqrt(b); return x;
}

public double normalRandom2(double a, double b) {
double pi = 3.1415926535;
double r1 = Math.random();
Math.random();
Math.random();
Math.random();
Math.random();
Math.random();
Math.random();
Math.random();
double r2 = Math.random();
double u = Math.sqrt((-2) * Math.log(r1)) * Math.cos(2 * pi * r2);
double z = a + u * Math.sqrt(b); return (z);
}

public double normalRandom3(double a, double b) {
double f = 0;
double c0 = 2.515517, c1 = 0.802853, c2 = 0.010328;
double d1 = 1.432788, d2 = 0.189269, d3 = 0.001308; double w;
double r = Math.random();
if (r <= 0.5) w = r;
else w = 1 - r;
if ((r - 0.5) > 0) f = 1;
else if ((r - 0.5) < 0) f = -1;
double y = Math.sqrt((-2) * Math.log(w));
double x = f * (y - (c0 + c1 * y + c2 * y * y) / (1 + d1 * y + d2 * y * y + d3 * y * y * y));
double z = a + x * Math.sqrt(b); return (z);
}

//然後判斷用哪個常態分佈隨機數函數的方法如下:
public double normalRandom(double a, double b) {
double r = Math.random() * 9;
switch ((int) r / 3) {
case 0: return normalRandom1(a, b);
case 1: return normalRandom2(a, b);
case 2: return normalRandom3(a, b);
}
return 0.0;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.