Java Gaussian RandGaussian()

Here you can find the source of RandGaussian()

Description

Rand Gaussian

License

Open Source License

Declaration

static public double RandGaussian() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    static private Random rand = new Random();
    static private double y2 = 0;
    static private boolean use_last = false;

    static public double RandGaussian() {
        return RandGaussian(0, 1);
    }//from www . j a  va 2 s  .  c o  m

    static public double RandGaussian(double mean, double standard_deviation) {

        double x1, x2, w, y1;

        if (use_last) /* use value from previous call */ {
            y1 = y2;
            use_last = false;
        } else {
            do {
                x1 = 2.0 * RandFloat() - 1.0;
                x2 = 2.0 * RandFloat() - 1.0;
                w = x1 * x1 + x2 * x2;
            } while (w >= 1.0);

            w = Math.sqrt((-2.0 * Math.log(w)) / w);
            y1 = x1 * w;
            y2 = x2 * w;
            use_last = true;
        }

        return (mean + y1 * standard_deviation);
    }

    static public double RandFloat() {
        return rand.nextDouble();
    }
}

Related

  1. addGaussianNoise(double[][] d, double m, double v)
  2. gaussian()
  3. getGaussian()
  4. getGaussian(double aMean, double aVariance)
  5. hashToGaussian(int h)