AbstractPcgMcg6432.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 Multiplicative Congruential Generator (MCG) and output
  22.  * 32-bits per cycle.
  23.  *
  24.  * @see <a href="http://www.pcg-random.org/">
  25.  *  PCG, A Family of Better Random Number Generators</a>
  26.  * @since 1.3
  27.  */
  28. abstract class AbstractPcgMcg6432 extends IntProvider {
  29.     /** The state of the MCG. */
  30.     private long state;

  31.     /**
  32.      * Creates a new instance.
  33.      *
  34.      * @param seed Initial seed.
  35.      */
  36.     AbstractPcgMcg6432(Long seed) {
  37.         // A seed of zero will result in a non-functional MCG; it must be odd for a maximal
  38.         // period MCG. The multiplication factor always sets the 2 least-significant bits to 1
  39.         // if they are already 1 so these are explicitly set. Bit k (zero-based) will have
  40.         // period 2^(k-1) starting from bit 2 with a period of 1. Bit 63 has period 2^62.
  41.         state = seed | 3;
  42.     }

  43.     /**
  44.      * Provides the next state of the MCG.
  45.      *
  46.      * @param input Current state.
  47.      * @return next state
  48.      */
  49.     private static long bump(long input) {
  50.         return input * 6364136223846793005L;
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public int next() {
  55.         final long x = state;
  56.         state = bump(state);
  57.         return transform(x);
  58.     }

  59.     /**
  60.      * Transform the 64-bit state of the generator to a 32-bit output.
  61.      * The transformation function shall vary with respect to different generators.
  62.      *
  63.      * @param x State.
  64.      * @return the output
  65.      */
  66.     protected abstract int transform(long x);

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     protected byte[] getStateInternal() {
  70.         return composeStateInternal(NumberFactory.makeByteArray(state),
  71.                 super.getStateInternal());
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     protected void setStateInternal(byte[] s) {
  76.         final byte[][] d = splitStateInternal(s, 8);
  77.         // As per the constructor, ensure the lower 2 bits of state are set.
  78.         state = NumberFactory.makeLong(d[0]) | 3;
  79.         super.setStateInternal(d[1]);
  80.     }
  81. }