1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * https://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 package org.hipparchus.random; 23 24 25 /** 26 * This class implements the WELL19937a pseudo-random number generator 27 * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. 28 * <p> 29 * This generator is described in a paper by François Panneton, 30 * Pierre L'Ecuyer and Makoto Matsumoto <a 31 * href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">Improved 32 * Long-Period Generators Based on Linear Recurrences Modulo 2</a> ACM 33 * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper 34 * are in <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt"> 35 * wellrng-errata.txt</a>. 36 * 37 * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a> 38 */ 39 public class Well19937a extends AbstractWell { 40 41 /** Serializable version identifier. */ 42 private static final long serialVersionUID = 20150223L; 43 44 /** Number of bits in the pool. */ 45 private static final int K = 19937; 46 47 /** First parameter of the algorithm. */ 48 private static final int M1 = 70; 49 50 /** Second parameter of the algorithm. */ 51 private static final int M2 = 179; 52 53 /** Third parameter of the algorithm. */ 54 private static final int M3 = 449; 55 56 /** The indirection index table. */ 57 private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3); 58 59 /** 60 * Creates a new random number generator. 61 * <p> 62 * The instance is initialized using the current time as the seed. 63 */ 64 public Well19937a() { 65 super(K); 66 } 67 68 /** 69 * Creates a new random number generator using a single int seed. 70 * @param seed the initial seed (32 bits integer) 71 */ 72 public Well19937a(int seed) { 73 super(K, seed); 74 } 75 76 /** 77 * Creates a new random number generator using an int array seed. 78 * @param seed the initial seed (32 bits integers array), if null 79 * the seed of the generator will be related to the current time 80 */ 81 public Well19937a(int[] seed) { 82 super(K, seed); 83 } 84 85 /** 86 * Creates a new random number generator using a single long seed. 87 * @param seed the initial seed (64 bits integer) 88 */ 89 public Well19937a(long seed) { 90 super(K, seed); 91 } 92 93 /** {@inheritDoc} */ 94 @Override 95 public int nextInt() { 96 97 final int indexRm1 = TABLE.getIndexPred(index); 98 final int indexRm2 = TABLE.getIndexPred2(index); 99 100 final int v0 = v[index]; 101 final int vM1 = v[TABLE.getIndexM1(index)]; 102 final int vM2 = v[TABLE.getIndexM2(index)]; 103 final int vM3 = v[TABLE.getIndexM3(index)]; 104 105 final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]); 106 final int z1 = (v0 ^ (v0 << 25)) ^ (vM1 ^ (vM1 >>> 27)); 107 final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1)); 108 final int z3 = z1 ^ z2; 109 final int z4 = z0 ^ (z1 ^ (z1 << 9)) ^ (z2 ^ (z2 << 21)) ^ (z3 ^ (z3 >>> 21)); 110 111 v[index] = z3; 112 v[indexRm1] = z4; 113 v[indexRm2] &= 0x80000000; 114 index = indexRm1; 115 116 return z4; 117 } 118 119 }