fork download
  1. package Lec11;
  2.  
  3. import java.util.*;
  4.  
  5. public class BoxMullerMethod {
  6.  
  7. private double Mean;
  8. private double StandardDeviation;
  9. private Random random = new Random();
  10. private boolean flag = true;
  11. private double x0;
  12. private double x1;
  13.  
  14. public BoxMullerMethod() {
  15. this(0.0, 1.0);
  16. }
  17.  
  18. public BoxMullerMethod(double mean, double standardDeviation) {
  19. this.Mean = mean;
  20. this.StandardDeviation = standardDeviation;
  21. }
  22.  
  23. public double normRand() {
  24. return Mean + StandardDeviation * nextGaussian();
  25. }
  26.  
  27. private double nextGaussian() {
  28. double multiplier;
  29.  
  30. if (flag) {
  31. x0 = random.nextDouble();
  32. x1 = random.nextDouble();
  33.  
  34. multiplier = Math.sin(2.0 * Math.PI * x1);
  35. flag = false;
  36. } else {
  37. multiplier = Math.cos(2.0 * Math.PI * x1);
  38. flag = true;
  39. }
  40.  
  41. return Math.sqrt(-2.0 * Math.log(x0)) * multiplier;
  42. }
  43. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty