Java method for generating Normal Distribution

Source: Internet
Author: User

// Normal distribution function. select one of the three generation methods randomly, which is more random.
Public classNormal {
Public normal (){
}

Public double normalrandom1 (double A, double B) {// note that B is the square of the variance, which is equal to the standard deviation.
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 ));
Double Z = a + x * Math. SQRT (B); Return (z );
}

// The method for determining which Normal Distribution Random Number function is used is as follows:
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;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.