Java Gauss gauss(int N, long seed)

Here you can find the source of gauss(int N, long seed)

Description

gauss

License

Open Source License

Declaration

public static double[] gauss(int N, long seed) 

Method Source Code

//package com.java2s;
/* Copyright 2009-2016 David Hadka
 *
 * This file is part of the MOEA Framework.
 *
 * The MOEA Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.//  ww  w . j  a  v a  2s.c om
 *
 * The MOEA Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the MOEA Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static double[] gauss(int N, long seed) {
        int i;
        double[] uniftmp = uniform(2 * N, seed);
        double[] g = new double[N];

        for (i = 0; i < N; i++) {
            g[i] = Math.sqrt(-2 * Math.log(uniftmp[i]))
                    * Math.cos(2 * Math.PI * uniftmp[N + i]);

            if (g[i] == 0.0) {
                g[i] = 1e-99;
            }
        }

        return g;
    }

    public static double[] uniform(int N, long inseed) {
        /* generates N uniform numbers with starting seed */
        long aktseed;
        int tmp;
        long[] rgrand = new long[32];
        long aktrand;
        int i;
        double[] r = new double[N];

        if (inseed < 0) {
            inseed = -inseed;
        }

        if (inseed < 1) {
            inseed = 1;
        }

        aktseed = inseed;

        for (i = 39; i >= 0; i--) {
            tmp = (int) Math.floor((double) aktseed / (double) 127773);
            aktseed = 16807 * (aktseed - tmp * 127773) - 2836 * tmp;

            if (aktseed < 0) {
                aktseed = aktseed + 2147483647;
            }

            if (i < 32) {
                rgrand[i] = aktseed;
            }
        }

        aktrand = rgrand[0];

        for (i = 0; i < N; i++) {
            tmp = (int) Math.floor((double) aktseed / (double) 127773);
            aktseed = 16807 * (aktseed - tmp * 127773) - 2836 * tmp;

            if (aktseed < 0) {
                aktseed = aktseed + 2147483647;
            }

            tmp = (int) Math.floor((double) aktrand / (double) 67108865);
            aktrand = rgrand[tmp];
            rgrand[tmp] = aktseed;
            r[i] = (double) aktrand / 2.147483647e9;

            if (r[i] == 0.0) {
                r[i] = 1e-99;
            }
        }
        return r;
    }
}

Related

  1. gauss(double mean, double deviation, double x)
  2. gauss(double[] A, int m, int n)
  3. gauss(final double mean, final double sigma, final double x)
  4. GaussElimination(double a[][])
  5. gaussian(double a[][], int index[])
  6. gaussian(double mu, double sigma, double x)
  7. gaussian(double mu, double sigma, double x)