001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.rng.core.source32;
018
019/**
020 * A Permuted Congruential Generator (PCG) that is composed of a 64-bit Linear Congruential
021 * Generator (LCG) combined with the XSH-RR (xorshift; random rotate) output
022 * transformation to create 32-bit output.
023 *
024 * <p>State size is 128 bits and the period is 2<sup>64</sup>.</p>
025 *
026 * <p><strong>Note:</strong> Although the seed size is 128 bits, only the first 64 are
027 * effective: in effect, two seeds that only differ by the last 64 bits may produce
028 * highly correlated sequences.
029 *
030 * @see <a href="http://www.pcg-random.org/">
031 *  PCG, A Family of Better Random Number Generators</a>
032 * @since 1.3
033 */
034public class PcgXshRr32 extends AbstractPcg6432 {
035    /**
036     * Creates a new instance using a default increment.
037     *
038     * @param seed Initial state.
039     * @since 1.4
040     */
041    public PcgXshRr32(Long seed) {
042        super(seed);
043    }
044
045    /**
046     * Creates a new instance.
047     *
048     * <p><strong>Note:</strong> Although the seed size is 128 bits, only the first 64 are
049     * effective: in effect, two seeds that only differ by the last 64 bits may produce
050     * highly correlated sequences.
051     *
052     * @param seed Initial seed.
053     * If the length is larger than 2, only the first 2 elements will
054     * be used; if smaller, the remaining elements will be automatically set.
055     *
056     * <p>The 1st element is used to set the LCG state. The 2nd element is used
057     * to set the LCG increment; the most significant bit
058     * is discarded by left shift and the increment is set to odd.</p>
059     */
060    public PcgXshRr32(long[] seed) {
061        super(seed);
062    }
063
064    /** {@inheritDoc} */
065    @Override
066    protected int transform(long x) {
067        final int count = (int)(x >>> 59);
068        return Integer.rotateRight((int)((x ^ (x >>> 18)) >>> 27), count);
069    }
070}