XoRoShiRo128PlusPlus.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.source64;

  18. import org.apache.commons.rng.JumpableUniformRandomProvider;
  19. import org.apache.commons.rng.UniformRandomProvider;

  20. /**
  21.  * A fast all-purpose 64-bit generator.
  22.  *
  23.  * <p>This is a member of the Xor-Shift-Rotate family of generators. Memory footprint is 128 bits
  24.  * and the period is 2<sup>128</sup>-1. Speed is expected to be similar to
  25.  * {@link XoShiRo256StarStar}.</p>
  26.  *
  27.  * @see <a href="http://xoshiro.di.unimi.it/xoroshiro128plusplus.c">Original source code</a>
  28.  * @see <a href="http://xoshiro.di.unimi.it/">xorshiro / xoroshiro generators</a>
  29.  * @since 1.3
  30.  */
  31. public class XoRoShiRo128PlusPlus extends AbstractXoRoShiRo128 {
  32.     /** The coefficients for the jump function. */
  33.     private static final long[] JUMP_COEFFICIENTS = {
  34.         0x2bd7a6a6e99c2ddcL, 0x0992ccaf6a6fca05L
  35.     };
  36.     /** The coefficients for the long jump function. */
  37.     private static final long[] LONG_JUMP_COEFFICIENTS = {
  38.         0x360fd5f2cf8d5d99L, 0x9c6e6877736c46e3L
  39.     };

  40.     /**
  41.      * Creates a new instance.
  42.      *
  43.      * @param seed Initial seed.
  44.      * If the length is larger than 2, only the first 2 elements will
  45.      * be used; if smaller, the remaining elements will be automatically
  46.      * set. A seed containing all zeros will create a non-functional generator.
  47.      */
  48.     public XoRoShiRo128PlusPlus(long[] seed) {
  49.         super(seed);
  50.     }

  51.     /**
  52.      * Creates a new instance using a 2 element seed.
  53.      * A seed containing all zeros will create a non-functional generator.
  54.      *
  55.      * @param seed0 Initial seed element 0.
  56.      * @param seed1 Initial seed element 1.
  57.      */
  58.     public XoRoShiRo128PlusPlus(long seed0, long seed1) {
  59.         super(seed0, seed1);
  60.     }

  61.     /**
  62.      * Creates a copy instance.
  63.      *
  64.      * @param source Source to copy.
  65.      */
  66.     protected XoRoShiRo128PlusPlus(XoRoShiRo128PlusPlus source) {
  67.         super(source);
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public long next() {
  72.         // Override the abstract class to use a different state update step.
  73.         // Note: This requires different jump coefficients.

  74.         final long s0 = state0;
  75.         long s1 = state1;
  76.         final long result = Long.rotateLeft(s0 + s1, 17) + s0;

  77.         s1 ^= s0;
  78.         state0 = Long.rotateLeft(s0, 49) ^ s1 ^ (s1 << 21); // a, b
  79.         state1 = Long.rotateLeft(s1, 28); // c

  80.         return result;
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     protected long nextOutput() {
  85.         throw new UnsupportedOperationException("The PlusPlus algorithm redefines the next() method");
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public UniformRandomProvider jump() {
  90.         // Duplicated from the abstract class to change the jump coefficients
  91.         final UniformRandomProvider copy = copy();
  92.         performJump(JUMP_COEFFICIENTS);
  93.         return copy;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public JumpableUniformRandomProvider longJump() {
  98.         // Duplicated from the abstract class to change the jump coefficients
  99.         final JumpableUniformRandomProvider copy = copy();
  100.         performJump(LONG_JUMP_COEFFICIENTS);
  101.         return copy;
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     protected XoRoShiRo128PlusPlus copy() {
  106.         // This exists to ensure the jump function performed in the super class returns
  107.         // the correct class type. It should not be public.
  108.         return new XoRoShiRo128PlusPlus(this);
  109.     }
  110. }