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 = seed; 057 c = seed; 058 d = seed; 059 for (int i = 0; i < 20; i++) { 060 next(); 061 } 062 } 063 064 /** {@inheritDoc} */ 065 @Override 066 public final int next() { 067 final int e = a - Integer.rotateLeft(b, 27); 068 a = b ^ Integer.rotateLeft(c, 17); 069 b = c + d; 070 c = d + e; 071 d = e + a; 072 return d; 073 } 074 075 /** {@inheritDoc} */ 076 @Override 077 protected byte[] getStateInternal() { 078 return composeStateInternal(NumberFactory.makeByteArray(new int[] {a, b, c, d}), 079 super.getStateInternal()); 080 } 081 082 /** {@inheritDoc} */ 083 @Override 084 protected void setStateInternal(byte[] s) { 085 final byte[][] parts = splitStateInternal(s, 4 * 4); 086 087 final int[] tmp = NumberFactory.makeIntArray(parts[0]); 088 a = tmp[0]; 089 b = tmp[1]; 090 c = tmp[2]; 091 d = tmp[3]; 092 093 super.setStateInternal(parts[1]); 094 } 095}