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

  20. /**
  21.  * Port from Marsaglia's <a href="https://en.wikipedia.org/wiki/Multiply-with-carry">
  22.  * "Multiply-With-Carry" algorithm</a>.
  23.  *
  24.  * <p>
  25.  * Implementation is based on the (non-portable!) C code reproduced on
  26.  * <a href="http://school.anhb.uwa.edu.au/personalpages/kwessen/shared/Marsaglia03.html">
  27.  * that page</a>.
  28.  * </p>
  29.  *
  30.  * @see <a href="https://en.wikipedia.org/wiki/Multiply-with-carry">Multiply with carry (Wikipedia)</a>
  31.  * @since 1.0
  32.  */
  33. public class MultiplyWithCarry256 extends IntProvider {
  34.     /** Length of the state array. */
  35.     private static final int Q_SIZE = 256;
  36.     /** Size of the seed. */
  37.     private static final int SEED_SIZE = Q_SIZE + 1;
  38.     /** Multiply. */
  39.     private static final long A = 809430660;
  40.     /** State. */
  41.     private final int[] state = new int[Q_SIZE];
  42.     /** Current index in "state" array. */
  43.     private int index;
  44.     /** Carry. */
  45.     private int carry;

  46.     /**
  47.      * Creates a new instance.
  48.      *
  49.      * @param seed Seed.
  50.      * If the length is larger than 257, only the first 257 elements will
  51.      * be used; if smaller, the remaining elements will be automatically
  52.      * set.
  53.      */
  54.     public MultiplyWithCarry256(int[] seed) {
  55.         setSeedInternal(seed);
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     protected byte[] getStateInternal() {
  60.         final int[] s = Arrays.copyOf(state, SEED_SIZE + 1);
  61.         s[SEED_SIZE - 1] = carry;
  62.         s[SEED_SIZE] = index;

  63.         return composeStateInternal(NumberFactory.makeByteArray(s),
  64.                                     super.getStateInternal());
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     protected void setStateInternal(byte[] s) {
  69.         final byte[][] c = splitStateInternal(s, (SEED_SIZE + 1) * 4);

  70.         final int[] tmp = NumberFactory.makeIntArray(c[0]);

  71.         System.arraycopy(tmp, 0, state, 0, Q_SIZE);
  72.         carry = tmp[SEED_SIZE - 1];
  73.         index = tmp[SEED_SIZE];

  74.         super.setStateInternal(c[1]);
  75.     }

  76.     /**
  77.      * Seeds the RNG.
  78.      *
  79.      * @param seed Seed.
  80.      */
  81.     private void setSeedInternal(int[] seed) {
  82.         // Reset the whole state of this RNG (i.e. "state" and "index").
  83.         // Filling procedure is not part of the reference code.
  84.         final int[] tmp = new int[SEED_SIZE];
  85.         fillState(tmp, seed);

  86.         // First element of the "seed" is the initial "carry".
  87.         final int c = tmp[0];
  88.         // Marsaglia's recommendation: 0 <= carry < A.
  89.         carry = (int) (Math.abs(c) % A);

  90.         // Initial state.
  91.         System.arraycopy(tmp, 1, state, 0, Q_SIZE);

  92.         // Initial index.
  93.         index = Q_SIZE;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public int next() {
  98.         // Produce an index in the range 0-255
  99.         index &= 0xff;
  100.         final long t = A * (state[index] & 0xffffffffL) + carry;
  101.         carry = (int) (t >> 32);
  102.         return state[index++] = (int) t;
  103.     }
  104. }