SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
NormalBoxMullerGen.java
1/*
2 * Class: NormalBoxMullerGen
3 * Description: normal random variate generators using the Box-Muller method
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.randvar;
26
27import umontreal.ssj.rng.*;
28import umontreal.ssj.probdist.*;
29
40public class NormalBoxMullerGen extends NormalGen {
41 private boolean available = false;
42 private double[] variates = new double[2];
43 private static double[] staticVariates = new double[2];
44 // used by polar method which calculate always two random values;
45
50 public NormalBoxMullerGen(RandomStream s, double mu, double sigma) {
51 super(s, null);
52 setParams(mu, sigma);
53 }
54
60 this(s, 0.0, 1.0);
61 }
62
68 super(s, dist);
69 if (dist != null)
70 setParams(dist.getMu(), dist.getSigma());
71 }
72
73 public double nextDouble() {
74 if (available) {
75 available = false;
76 return mu + sigma * variates[1];
77 } else {
78 boxMuller(stream, mu, sigma, variates);
79 available = true;
80 return mu + sigma * variates[0];
81 }
82 }
83
89 public static double nextDouble(RandomStream s, double mu, double sigma) {
90 boxMuller(s, mu, sigma, staticVariates);
91 return mu + sigma * staticVariates[0];
92 }
93//>>>>>>>>>>>>>>>>>>>> P R I V A T E S M E T H O D S <<<<<<<<<<<<<<<<<<<<
94
95 private static void boxMuller(RandomStream stream, double mu, double sigma, double[] variates) {
96 final double pi = Math.PI;
97 double u, v, s;
98
99 u = stream.nextDouble();
100 v = stream.nextDouble();
101 s = Math.sqrt(-2.0 * Math.log(u));
102 variates[1] = s * Math.sin(2 * pi * v);
103 variates[0] = s * Math.cos(2 * pi * v);
104 }
105
106}
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
NormalBoxMullerGen(RandomStream s, NormalDist dist)
Creates a random variate generator for the normal distribution dist and stream s.
NormalBoxMullerGen(RandomStream s)
Creates a standard normal random variate generator with mean 0 and standard deviation 1,...
static double nextDouble(RandomStream s, double mu, double sigma)
Generates a variate from the normal distribution with parameters &#160;mu and &#160;sigma,...
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
NormalBoxMullerGen(RandomStream s, double mu, double sigma)
Creates a normal random variate generator with mean mu and standard deviation sigma,...
void setParams(double mu, double sigma)
Sets the parameters and of this object.
NormalGen(RandomStream s, double mu, double sigma)
Creates a normal random variate generator with mean mu and standard deviation sigma,...
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...