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 Bob Jenkins's small fast (JSF) 32-bit generator.
023 *
024 * <p>The state size is 128-bits; the shortest period is expected to be about 2<sup>94</sup>
025 * and it expected that about one seed will run into another seed within 2<sup>64</sup> values.</p>
026 *
027 * @see <a href="https://burtleburtle.net/bob/rand/smallprng.html">A small noncryptographic PRNG</a>
028 * @since 1.3
029 */
030public class JenkinsSmallFast32 extends IntProvider {
031    /** State a. */
032    private int a;
033    /** State b. */
034    private int b;
035    /** State c. */
036    private int c;
037    /** Statd d. */
038    private int d;
039
040    /**
041     * Creates an instance with the given seed.
042     *
043     * @param seed Initial seed.
044     */
045    public JenkinsSmallFast32(Integer seed) {
046        setSeedInternal(seed);
047    }
048
049    /**
050     * Seeds the RNG.
051     *
052     * @param seed Seed.
053     */
054    private void setSeedInternal(int seed) {
055        a = 0xf1ea5eed;
056        b = c = d = seed;
057        for (int i = 0; i < 20; i++) {
058            next();
059        }
060    }
061
062    /** {@inheritDoc} */
063    @Override
064    public final int next() {
065        final int e = a - Integer.rotateLeft(b, 27);
066        a = b ^ Integer.rotateLeft(c, 17);
067        b = c + d;
068        c = d + e;
069        d = e + a;
070        return d;
071    }
072
073    /** {@inheritDoc} */
074    @Override
075    protected byte[] getStateInternal() {
076        return composeStateInternal(NumberFactory.makeByteArray(new int[] {a, b, c, d}),
077                                    super.getStateInternal());
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    protected void setStateInternal(byte[] s) {
083        final byte[][] parts = splitStateInternal(s, 4 * 4);
084
085        final int[] tmp = NumberFactory.makeIntArray(parts[0]);
086        a = tmp[0];
087        b = tmp[1];
088        c = tmp[2];
089        d = tmp[3];
090
091        super.setStateInternal(parts[1]);
092    }
093}