package Lec11;
import java.util.*;
public class BoxMullerMethod {
private double Mean;
private double StandardDeviation;
private boolean flag = true;
private double x0;
private double x1;
public BoxMullerMethod() {
this(0.0, 1.0);
}
public BoxMullerMethod(double mean, double standardDeviation) {
this.Mean = mean;
this.StandardDeviation = standardDeviation;
}
public double normRand() {
return Mean + StandardDeviation * nextGaussian();
}
private double nextGaussian() {
double multiplier;
if (flag) {
x0 = random.nextDouble();
x1 = random.nextDouble();
multiplier
= Math.
sin(2.0 * Math.
PI * x1
); flag = false;
} else {
multiplier
= Math.
cos(2.0 * Math.
PI * x1
); flag = true;
}
return Math.
sqrt(-2.0 * Math.
log(x0
)) * multiplier
; }
}
cGFja2FnZSBMZWMxMTsKCmltcG9ydCBqYXZhLnV0aWwuKjsKCnB1YmxpYyBjbGFzcyBCb3hNdWxsZXJNZXRob2QgewoKICAgIHByaXZhdGUgZG91YmxlIE1lYW47CiAgICBwcml2YXRlIGRvdWJsZSBTdGFuZGFyZERldmlhdGlvbjsKICAgIHByaXZhdGUgUmFuZG9tIHJhbmRvbSA9IG5ldyBSYW5kb20oKTsKICAgIHByaXZhdGUgYm9vbGVhbiBmbGFnID0gdHJ1ZTsKICAgIHByaXZhdGUgZG91YmxlIHgwOwogICAgcHJpdmF0ZSBkb3VibGUgeDE7CgogICAgcHVibGljIEJveE11bGxlck1ldGhvZCgpIHsKICAgICAgICB0aGlzKDAuMCwgMS4wKTsKICAgIH0KCiAgICBwdWJsaWMgQm94TXVsbGVyTWV0aG9kKGRvdWJsZSBtZWFuLCBkb3VibGUgc3RhbmRhcmREZXZpYXRpb24pIHsKICAgICAgICB0aGlzLk1lYW4gPSBtZWFuOwogICAgICAgIHRoaXMuU3RhbmRhcmREZXZpYXRpb24gPSBzdGFuZGFyZERldmlhdGlvbjsKICAgIH0KCiAgICBwdWJsaWMgZG91YmxlIG5vcm1SYW5kKCkgewogICAgICAgIHJldHVybiBNZWFuICsgU3RhbmRhcmREZXZpYXRpb24gKiBuZXh0R2F1c3NpYW4oKTsKICAgIH0KCiAgICBwcml2YXRlIGRvdWJsZSBuZXh0R2F1c3NpYW4oKSB7CiAgICAgICAgZG91YmxlIG11bHRpcGxpZXI7CgogICAgICAgIGlmIChmbGFnKSB7CiAgICAgICAgICAgIHgwID0gcmFuZG9tLm5leHREb3VibGUoKTsKICAgICAgICAgICAgeDEgPSByYW5kb20ubmV4dERvdWJsZSgpOwoKICAgICAgICAgICAgbXVsdGlwbGllciA9IE1hdGguc2luKDIuMCAqIE1hdGguUEkgKiB4MSk7CiAgICAgICAgICAgIGZsYWcgPSBmYWxzZTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBtdWx0aXBsaWVyID0gTWF0aC5jb3MoMi4wICogTWF0aC5QSSAqIHgxKTsKICAgICAgICAgICAgZmxhZyA9IHRydWU7CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gTWF0aC5zcXJ0KC0yLjAgKiBNYXRoLmxvZyh4MCkpICogbXVsdGlwbGllcjsKICAgIH0KfQ==