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
019import org.apache.commons.rng.core.util.NumberFactory;
020
021/**
022 * Implement the Small, Fast, Counting (SFC) 32-bit generator of Chris Doty-Humphrey.
023 * The original source is the PractRand test suite by the same author.
024 *
025 * <p>The state size is 128-bits; the period is a minimum of 2<sup>32</sup> and an
026 * average of approximately 2<sup>127</sup>.</p>
027 *
028 * @see <a href="https://pracrand.sourceforge.net/">PractRand</a>
029 * @since 1.3
030 */
031public class DotyHumphreySmallFastCounting32 extends IntProvider {
032    /** Size of the seed. */
033    private static final int SEED_SIZE = 3;
034
035    /** State a. */
036    private int a;
037    /** State b. */
038    private int b;
039    /** State c. */
040    private int c;
041    /** Counter. */
042    private int counter;
043
044    /**
045     * Creates an instance with the given seed.
046     *
047     * @param seed Initial seed.
048     * If the length is larger than 3, only the first 3 elements will
049     * be used; if smaller, the remaining elements will be automatically set.
050     */
051    public DotyHumphreySmallFastCounting32(int[] seed) {
052        setSeedInternal(extendSeed(seed, SEED_SIZE));
053    }
054
055    /**
056     * Seeds the RNG.
057     *
058     * @param seed Seed.
059     */
060    private void setSeedInternal(int[] seed) {
061        a = seed[0];
062        b = seed[1];
063        c = seed[2];
064        counter = 1;
065        for (int i = 0; i < 15; i++) {
066            next();
067        }
068    }
069
070    /** {@inheritDoc} */
071    @Override
072    public final int next() {
073        final int tmp = a + b + counter++;
074        a = b ^ (b >>> 9);
075        b = c + (c << 3);
076        c = Integer.rotateLeft(c, 21) + tmp;
077        return tmp;
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    protected byte[] getStateInternal() {
083        return composeStateInternal(NumberFactory.makeByteArray(new int[] {a, b, c, counter}),
084                                    super.getStateInternal());
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    protected void setStateInternal(byte[] s) {
090        final byte[][] parts = splitStateInternal(s, 4 * 4);
091
092        final int[] tmp = NumberFactory.makeIntArray(parts[0]);
093        a = tmp[0];
094        b = tmp[1];
095        c = tmp[2];
096        counter = tmp[3];
097
098        super.setStateInternal(parts[1]);
099    }
100}