1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/* 2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more 3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements. See the NOTICE file distributed with 4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership. 5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0 6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with 7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License. You may obtain a copy of the License at 8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * http://www.apache.org/licenses/LICENSE-2.0 10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software 12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS, 13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and 15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License. 16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.random; 18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.NotStrictlyPositiveException; 20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Abstract class implementing the {@link RandomGenerator} interface. 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Default implementations for all methods other than {@link #nextDouble()} and 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link #setSeed(long)} are provided. 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * All data generation methods are based on {@code code nextDouble()}. 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Concrete implementations <strong>must</strong> override 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this method and <strong>should</strong> provide better / more 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * performant implementations of the other methods if the underlying PRNG 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * supplies them.</p> 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.1 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $ 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic abstract class AbstractRandomGenerator implements RandomGenerator { 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Cached random normal value. The default implementation for 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link #nextGaussian} generates pairs of values and this field caches the 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * second value so that the full algorithm is not executed for every 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * activation. The value {@code Double.NaN} signals that there is 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * no cached value. Use {@link #clear} to clear the cached value. 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private double cachedNormalDeviate = Double.NaN; 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Construct a RandomGenerator. 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public AbstractRandomGenerator() { 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(); 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Clears the cache used by the default implementation of 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link #nextGaussian}. Implemementations that do not override the 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * default implementation of {@code nextGaussian} should call this 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * method in the implementation of {@link #setSeed(long)} 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void clear() { 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond cachedNormalDeviate = Double.NaN; 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void setSeed(int seed) { 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond setSeed((long) seed); 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void setSeed(int[] seed) { 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // the following number is the largest prime that fits in 32 bits (it is 2^32 - 5) 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final long prime = 4294967291l; 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond long combined = 0l; 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int s : seed) { 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond combined = combined * prime + s; 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond setSeed(combined); 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Sets the seed of the underyling random number generator using a 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code long} seed. Sequences of values generated starting with the 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * same seeds should be identical. 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Implementations that do not override the default implementation of 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code nextGaussian} should include a call to {@link #clear} in the 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * implementation of this method.</p> 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param seed the seed value 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public abstract void setSeed(long seed); 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Generates random bytes and places them into a user-supplied 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * byte array. The number of random bytes produced is equal to 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the length of the byte array. 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The default implementation fills the array with bytes extracted from 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * random integers generated using {@link #nextInt}.</p> 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param bytes the non-null byte array in which to put the 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * random bytes 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void nextBytes(byte[] bytes) { 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int bytesOut = 0; 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond while (bytesOut < bytes.length) { 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int randInt = nextInt(); 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < 3; i++) { 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if ( i > 0) { 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond randInt = randInt >> 8; 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond bytes[bytesOut++] = (byte) randInt; 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (bytesOut == bytes.length) { 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return; 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the next pseudorandom, uniformly distributed {@code int} 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * value from this random number generator's sequence. 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * All 2<font size="-1"><sup>32</sup></font> possible {@code int} values 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * should be produced with (approximately) equal probability. 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The default implementation provided here returns 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>(int) (nextDouble() * Integer.MAX_VALUE)</code> 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the next pseudorandom, uniformly distributed {@code int} 134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * value from this random number generator's sequence 135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public int nextInt() { 137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return (int) (nextDouble() * Integer.MAX_VALUE); 138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns a pseudorandom, uniformly distributed {@code int} value 142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * between 0 (inclusive) and the specified value (exclusive), drawn from 143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this random number generator's sequence. 144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The default implementation returns 146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>(int) (nextDouble() * n</code> 148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param n the bound on the random number to be returned. Must be 151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * positive. 152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a pseudorandom, uniformly distributed {@code int} 153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * value between 0 (inclusive) and n (exclusive). 154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NotStrictlyPositiveException if {@code n <= 0}. 155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public int nextInt(int n) { 157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (n <= 0 ) { 158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new NotStrictlyPositiveException(n); 159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int result = (int) (nextDouble() * n); 161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return result < n ? result : n - 1; 162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the next pseudorandom, uniformly distributed {@code long} 166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * value from this random number generator's sequence. All 167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 2<font size="-1"><sup>64</sup></font> possible {@code long} values 168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * should be produced with (approximately) equal probability. 169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The default implementation returns 171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>(long) (nextDouble() * Long.MAX_VALUE)</code> 173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the next pseudorandom, uniformly distributed {@code long} 176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *value from this random number generator's sequence 177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public long nextLong() { 179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return (long) (nextDouble() * Long.MAX_VALUE); 180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the next pseudorandom, uniformly distributed 184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code boolean} value from this random number generator's 185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * sequence. 186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The default implementation returns 188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>nextDouble() <= 0.5</code> 190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the next pseudorandom, uniformly distributed 193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code boolean} value from this random number generator's 194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * sequence 195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public boolean nextBoolean() { 197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return nextDouble() <= 0.5; 198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the next pseudorandom, uniformly distributed {@code float} 202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * value between {@code 0.0} and {@code 1.0} from this random 203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * number generator's sequence. 204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The default implementation returns 206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>(float) nextDouble() </code> 208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the next pseudorandom, uniformly distributed {@code float} 211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * value between {@code 0.0} and {@code 1.0} from this 212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * random number generator's sequence 213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public float nextFloat() { 215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return (float) nextDouble(); 216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the next pseudorandom, uniformly distributed 220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code double} value between {@code 0.0} and 221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code 1.0} from this random number generator's sequence. 222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This method provides the underlying source of random data used by the 224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * other methods.</p> 225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the next pseudorandom, uniformly distributed 227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code double} value between {@code 0.0} and 228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code 1.0} from this random number generator's sequence 229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public abstract double nextDouble(); 231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the next pseudorandom, Gaussian ("normally") distributed 234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code double} value with mean {@code 0.0} and standard 235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * deviation {@code 1.0} from this random number generator's sequence. 236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 237dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The default implementation uses the <em>Polar Method</em> 238dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * due to G.E.P. Box, M.E. Muller and G. Marsaglia, as described in 239dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * D. Knuth, <u>The Art of Computer Programming</u>, 3.4.1C.</p> 240dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 241dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The algorithm generates a pair of independent random values. One of 242dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * these is cached for reuse, so the full algorithm is not executed on each 243dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * activation. Implementations that do not override this method should 244dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * make sure to call {@link #clear} to clear the cached value in the 245dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * implementation of {@link #setSeed(long)}.</p> 246dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 247dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the next pseudorandom, Gaussian ("normally") distributed 248dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@code double} value with mean {@code 0.0} and 249dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * standard deviation {@code 1.0} from this random number 250dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * generator's sequence 251dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 252dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double nextGaussian() { 253dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (!Double.isNaN(cachedNormalDeviate)) { 254dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double dev = cachedNormalDeviate; 255dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond cachedNormalDeviate = Double.NaN; 256dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return dev; 257dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 258dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double v1 = 0; 259dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double v2 = 0; 260dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double s = 1; 261dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond while (s >=1 ) { 262dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond v1 = 2 * nextDouble() - 1; 263dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond v2 = 2 * nextDouble() - 1; 264dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond s = v1 * v1 + v2 * v2; 265dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 266dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (s != 0) { 267dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond s = FastMath.sqrt(-2 * FastMath.log(s) / s); 268dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 269dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond cachedNormalDeviate = v2 * s; 270dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return v1 * s; 271dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 272dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 273