PcgXshRr32.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. /**
  19.  * A Permuted Congruential Generator (PCG) that is composed of a 64-bit Linear Congruential
  20.  * Generator (LCG) combined with the XSH-RR (xorshift; random rotate) output
  21.  * transformation to create 32-bit output.
  22.  *
  23.  * <p>State size is 128 bits and the period is 2<sup>64</sup>.</p>
  24.  *
  25.  * <p><strong>Note:</strong> Although the seed size is 128 bits, only the first 64 are
  26.  * effective: in effect, two seeds that only differ by the last 64 bits may produce
  27.  * highly correlated sequences.
  28.  *
  29.  * @see <a href="http://www.pcg-random.org/">
  30.  *  PCG, A Family of Better Random Number Generators</a>
  31.  * @since 1.3
  32.  */
  33. public class PcgXshRr32 extends AbstractPcg6432 {
  34.     /**
  35.      * Creates a new instance using a default increment.
  36.      *
  37.      * @param seed Initial state.
  38.      * @since 1.4
  39.      */
  40.     public PcgXshRr32(Long seed) {
  41.         super(seed);
  42.     }

  43.     /**
  44.      * Creates a new instance.
  45.      *
  46.      * <p><strong>Note:</strong> Although the seed size is 128 bits, only the first 64 are
  47.      * effective: in effect, two seeds that only differ by the last 64 bits may produce
  48.      * highly correlated sequences.
  49.      *
  50.      * @param seed Initial seed.
  51.      * If the length is larger than 2, only the first 2 elements will
  52.      * be used; if smaller, the remaining elements will be automatically set.
  53.      *
  54.      * <p>The 1st element is used to set the LCG state. The 2nd element is used
  55.      * to set the LCG increment; the most significant bit
  56.      * is discarded by left shift and the increment is set to odd.</p>
  57.      */
  58.     public PcgXshRr32(long[] seed) {
  59.         super(seed);
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     protected int transform(long x) {
  64.         final int count = (int)(x >>> 59);
  65.         return Integer.rotateRight((int)((x ^ (x >>> 18)) >>> 27), count);
  66.     }
  67. }