AbstractPcg6432.java

  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. import org.apache.commons.rng.core.util.NumberFactory;

  19. /**
  20.  * This abstract class is a base for algorithms from the Permuted Congruential Generator (PCG)
  21.  * family that use an internal 64-bit Linear Congruential Generator (LCG) and output 32-bits
  22.  * per cycle.
  23.  *
  24.  * <h2>Note: PCG generators may exhibit massive stream correlation</h2>
  25.  *
  26.  * <p>Although the seed size is 128 bits, only the first 64 are effective: in effect,
  27.  * two seeds that only differ by the last 64 bits may produce highly correlated sequences.
  28.  *
  29.  * <p>Due to the use of an underlying linear congruential generator (LCG) alterations
  30.  * to the 128 bit seed have the following effect: the first 64-bits alter the
  31.  * generator state; the second 64 bits, with the exception of the most significant bit,
  32.  * which is discarded, choose between one of two alternative LCGs
  33.  * where the output of the chosen LCG is the same sequence except for an additive
  34.  * constant determined by the seed bits. The result is that seeds that differ
  35.  * only in the last 64-bits will have a 50% chance of producing highly correlated
  36.  * output sequences.

  37.  * <p>Consider using the fixed increment variant where the 64-bit seed sets the
  38.  * generator state.
  39.  *
  40.  * <p>For further information see:
  41.  * <ul>
  42.  *  <li>
  43.  *   <blockquote>
  44.  *    Durst, M.J. (1989) <i>Using Linear Congruential Generators For Parallel Random Number Generation.
  45.  *    Section 3.1: Different additive constants in a maximum potency congruential generator</i>.
  46.  *    1989 Winter Simulation Conference Proceedings, Washington, DC, USA, 1989, pp. 462-466.
  47.  *   </blockquote>
  48.  *  </li>
  49.  * </ul>
  50.  *
  51.  * @see <a href="http://www.pcg-random.org/">
  52.  *  PCG, A Family of Better Random Number Generators</a>
  53.  * @see <a href="https://ieeexplore.ieee.org/document/718715">Durst, M.J. (1989)
  54.  *  Using Linear Congruential Generators For Parallel Random Number Generation</a>
  55.  * @see <a href="https://issues.apache.org/jira/browse/RNG-123">
  56.  *  PCG generators may exhibit massive stream correlation</a>
  57.  * @since 1.3
  58.  */
  59. abstract class AbstractPcg6432 extends IntProvider {
  60.     /** Size of the seed array. */
  61.     private static final int SEED_SIZE = 2;
  62.     /** The default increment. */
  63.     private static final long DEFAULT_INCREMENT = 1442695040888963407L;

  64.     /** The state of the LCG. */
  65.     private long state;

  66.     /** The increment of the LCG. */
  67.     private long increment;

  68.     /**
  69.      * Creates a new instance using a default increment.
  70.      *
  71.      * @param seed Initial state.
  72.      * @since 1.4
  73.      */
  74.     AbstractPcg6432(Long seed) {
  75.         increment = DEFAULT_INCREMENT;
  76.         state = bump(seed + this.increment);
  77.     }

  78.     /**
  79.      * Creates a new instance.
  80.      *
  81.      * @param seed Initial seed.
  82.      * If the length is larger than 2, only the first 2 elements will
  83.      * be used; if smaller, the remaining elements will be automatically set.
  84.      *
  85.      * <p>The 1st element is used to set the LCG state. The 2nd element is used
  86.      * to set the LCG increment; the most significant bit
  87.      * is discarded by left shift and the increment is set to odd.</p>
  88.      */
  89.     AbstractPcg6432(long[] seed) {
  90.         if (seed.length < SEED_SIZE) {
  91.             final long[] tmp = new long[SEED_SIZE];
  92.             fillState(tmp, seed);
  93.             setSeedInternal(tmp);
  94.         } else {
  95.             setSeedInternal(seed);
  96.         }
  97.     }

  98.     /**
  99.      * Seeds the RNG.
  100.      *
  101.      * @param seed Seed.
  102.      */
  103.     private void setSeedInternal(long[] seed) {
  104.         // Ensure the increment is odd to provide a maximal period LCG.
  105.         this.increment = (seed[1] << 1) | 1;
  106.         this.state = bump(seed[0] + this.increment);
  107.     }

  108.     /**
  109.      * Provides the next state of the LCG.
  110.      *
  111.      * @param input Current state.
  112.      * @return next state
  113.      */
  114.     private long bump(long input) {
  115.         return input * 6364136223846793005L + increment;
  116.     }

  117.     /** {@inheritDoc} */
  118.     @Override
  119.     public int next() {
  120.         final long x = state;
  121.         state = bump(state);
  122.         return transform(x);
  123.     }

  124.     /**
  125.      * Transform the 64-bit state of the generator to a 32-bit output.
  126.      * The transformation function shall vary with respect to different generators.
  127.      *
  128.      * @param x State.
  129.      * @return the output
  130.      */
  131.     protected abstract int transform(long x);

  132.     /** {@inheritDoc} */
  133.     @Override
  134.     protected byte[] getStateInternal() {
  135.         // The increment is divided by 2 before saving.
  136.         // This transform is used in the reference PCG code; it prevents restoring from
  137.         // a byte state a non-odd increment that results in a sub-maximal period generator.
  138.         return composeStateInternal(NumberFactory.makeByteArray(
  139.                 new long[] {state, increment >>> 1}),
  140.                 super.getStateInternal());
  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     protected void setStateInternal(byte[] s) {
  145.         final byte[][] c = splitStateInternal(s, SEED_SIZE * 8);
  146.         final long[] tempseed = NumberFactory.makeLongArray(c[0]);
  147.         state = tempseed[0];
  148.         // Reverse the transform performed during getState to make the increment odd again.
  149.         increment = tempseed[1] << 1 | 1;
  150.         super.setStateInternal(c[1]);
  151.     }
  152. }