umontreal.iro.lecuyer.randvar.NormalGen Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ssj Show documentation
Show all versions of ssj Show documentation
SSJ is a Java library for stochastic simulation, developed under the direction of Pierre L'Ecuyer,
in the Département d'Informatique et de Recherche Opérationnelle (DIRO), at the Université de Montréal.
It provides facilities for generating uniform and nonuniform random variates, computing different
measures related to probability distributions, performing goodness-of-fit tests, applying quasi-Monte
Carlo methods, collecting (elementary) statistics, and programming discrete-event simulations with both
events and processes.
The newest version!
/*
* Class: NormalGen
* Description: random variates generator from the normal distribution
* Environment: Java
* Software: SSJ
* Copyright (C) 2001 Pierre L'Ecuyer and Université de Montréal
* Organization: DIRO, Université de Montréal
* @author
* @since
* SSJ is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License (GPL) as published by the
* Free Software Foundation, either version 3 of the License, or
* any later version.
* SSJ 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 General Public License for more details.
* A copy of the GNU General Public License is available at
GPL licence site.
*/
package umontreal.iro.lecuyer.randvar;
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
/**
* This class implements methods for generating random variates from the
* normal distribution
* N(μ, σ).
* It has mean μ and variance σ2, where σ > 0.
* Its density function is
*
*
*
* f (x) = 1/(2π)1/2σe(x-μ)2/(2σ2)
*
* The nextDouble method simply calls inverseF on the
* distribution.
*
*
* The following table gives the CPU time needed to generate 108 standard
* normal random variates using the different implementations available in SSJ.
* The first time is for a generator object (non-static method), and
* the second time is for the static method
* where no object is created.
* These tests were made on a machine with processor AMD Athlon 4000, running
* Red Hat Linux, with clock speed at 2403 MHz. The static method
* nextDouble() for NormalBoxMullerGen and
* NormalPolarGen uses only one number out of two that are
* generated; thus they are twice slower than the non-static method.
*
*
*
*
* Generator
* time in seconds
* time in seconds
*
*
* (object)
* (static)
*
* NormalGen
* 7.67
* 7.72
*
* NormalACRGen
* 4.71
* 4.76
*
* NormalBoxMullerGen
* 16.07
* 31.45
*
* NormalPolarGen
* 7.31
* 13.74
*
* NormalKindermannRamageGen
* 5.38
* 5.34
*
*
*
*
*/
public class NormalGen extends RandomVariateGen {
protected double mu;
protected double sigma = -1.0;
/**
* Creates a normal random variate generator with mean mu
* and standard deviation sigma, using stream s.
*
*/
public NormalGen (RandomStream s, double mu, double sigma) {
super (s, new NormalDist(mu, sigma));
setParams (mu, sigma);
}
/**
* Creates a standard normal random variate generator with mean
* 0 and standard deviation 1, using stream s.
*
*/
public NormalGen (RandomStream s) {
this (s, 0.0, 1.0);
}
/**
* Creates a random variate generator for the normal distribution
* dist and stream s.
*
*/
public NormalGen (RandomStream s, NormalDist dist) {
super (s, dist);
if (dist != null)
setParams (dist.getMu(), dist.getSigma());
}
/**
* Generates a variate from the normal distribution with
* parameters μ = mu and σ = sigma, using
* stream s.
*
*/
public static double nextDouble (RandomStream s, double mu, double sigma) {
return NormalDist.inverseF (mu, sigma, s.nextDouble());
}
/**
* Returns the parameter μ of this object.
*
*/
public double getMu() {
return mu;
}
/**
* Returns the parameter σ of this object.
*
*
*/
public double getSigma() {
return sigma;
}
/**
* Sets the parameters μ and σ of this object.
*
*/
protected void setParams (double mu, double sigma) {
if (sigma <= 0)
throw new IllegalArgumentException ("sigma <= 0");
this.mu = mu;
this.sigma = sigma;
}
}