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 * http://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 package org.apache.commons.rng.core.source32; 18 19 /** 20 * This class implements the WELL19937a pseudo-random number generator 21 * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. 22 * <p> 23 * This generator is described in a paper by François Panneton, 24 * Pierre L'Ecuyer and Makoto Matsumoto 25 * <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf"> 26 * Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a> 27 * ACM Transactions on Mathematical Software, 32, 1 (2006). 28 * The errata for the paper are in 29 * <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>. 30 * </p> 31 * 32 * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a> 33 * @since 1.0 34 */ 35 public class Well19937a extends AbstractWell { 36 /** Number of bits in the pool. */ 37 private static final int K = 19937; 38 /** First parameter of the algorithm. */ 39 private static final int M1 = 70; 40 /** Second parameter of the algorithm. */ 41 private static final int M2 = 179; 42 /** Third parameter of the algorithm. */ 43 private static final int M3 = 449; 44 /** The indirection index table. */ 45 private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3); 46 47 /** 48 * Creates a new random number generator. 49 * 50 * @param seed Initial seed. 51 */ 52 public Well19937a(int[] seed) { 53 super(K, seed); 54 } 55 56 /** {@inheritDoc} */ 57 @Override 58 public int next() { 59 final int indexRm1 = TABLE.getIndexPred(index); 60 final int indexRm2 = TABLE.getIndexPred2(index); 61 62 final int v0 = v[index]; 63 final int vM1 = v[TABLE.getIndexM1(index)]; 64 final int vM2 = v[TABLE.getIndexM2(index)]; 65 final int vM3 = v[TABLE.getIndexM3(index)]; 66 67 final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]); 68 final int z1 = (v0 ^ (v0 << 25)) ^ (vM1 ^ (vM1 >>> 27)); 69 final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1)); 70 final int z3 = z1 ^ z2; 71 final int z4 = z0 ^ (z1 ^ (z1 << 9)) ^ (z2 ^ (z2 << 21)) ^ (z3 ^ (z3 >>> 21)); 72 73 v[index] = z3; 74 v[indexRm1] = z4; 75 v[indexRm2] &= 0x80000000; 76 index = indexRm1; 77 78 return z4; 79 } 80 }