SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
NormalPolarGen.java
1/*
2 * Class: NormalPolarGen
3 * Description: normal random variate generators using the polar 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 NormalPolarGen extends NormalGen {
41
42 // used by polar method which calculate always two random values;
43 private boolean available = false;
44 private double[] variates = new double[2];
45 private static double[] staticVariates = new double[2];
46
51 public NormalPolarGen(RandomStream s, double mu, double sigma) {
52 super(s, null);
53 setParams(mu, sigma);
54 }
55
61 this(s, 0.0, 1.0);
62 }
63
69 super(s, dist);
70 if (dist != null)
71 setParams(dist.getMu(), dist.getSigma());
72 }
73
74 public double nextDouble() {
75 if (available) {
76 available = false;
77 return mu + sigma * variates[1];
78 } else {
79 polar(stream, mu, sigma, variates);
80 available = true;
81 return mu + sigma * variates[0];
82 }
83 }
84
90 public static double nextDouble(RandomStream s, double mu, double sigma) {
91 polar(s, mu, sigma, staticVariates);
92 return mu + sigma * staticVariates[0];
93 }
94
95//>>>>>>>>>>>>>>>>>>>> P R I V A T E M E T H O D S <<<<<<<<<<<<<<<<<<<<
96 // Polar method with rejection
97 private static void polar(RandomStream stream, double mu, double sigma, double[] variates) {
98 double x, y, s;
99 do {
100 x = 2 * stream.nextDouble() - 1;
101 y = 2 * stream.nextDouble() - 1;
102 s = x * x + y * y;
103 } while (s > 1.0 || s == 0.0);
104
105 double temp = Math.sqrt(-2.0 * Math.log(s) / s);
106
107 variates[0] = y * temp;
108 variates[1] = x * temp;
109 }
110
111}
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
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,...
NormalPolarGen(RandomStream s, double mu, double sigma)
Creates a normal random variate generator with mean mu and standard deviation sigma,...
NormalPolarGen(RandomStream s, NormalDist dist)
Creates a random variate generator for the normal distribution dist and stream s.
NormalPolarGen(RandomStream s)
Creates a standard normal random variate generator with and , using stream s.
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.
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,...