/** * Seeded constructor uses DiverRNG, which is of high quality, but low period (which rarely matters for games), * and has good speed and tiny state size. */ public StatefulRNG(long seed) { this(new DiverRNG(seed)); } /**
public MummyNoise(final int seed) { seedX = 0x9E3779B97F4A7C15L * DiverRNG.determine(seed + 0xC6BC279692B5CC83L); seedY = 0x9E3779B97F4A7C15L * DiverRNG.determine(seedX ^ 0xC7BC279692B5CB83L); seedZ = 0x9E3779B97F4A7C15L * DiverRNG.determine(seedY ^ 0xC8BC279692B5CA83L); seedW = 0x9E3779B97F4A7C15L * DiverRNG.determine(seedZ ^ 0xC9BC279692B5C983L); seedU = 0x9E3779B97F4A7C15L * DiverRNG.determine(seedW ^ 0xCABC279692B5C883L); seedV = 0x9E3779B97F4A7C15L * DiverRNG.determine(seedU ^ 0xCBBC279692B5C783L); }
/** * Inclusive inner, exclusive outer. * * @param inner the inner bound, inclusive, can be positive or negative * @param outer the outer bound, exclusive, can be positive or negative, usually greater than inner * @return a random int between inner (inclusive) and outer (exclusive) */ public final int nextInt(final int inner, final int outer) { return inner + nextInt(outer - inner); }
/** * Shuffle an array using the Fisher-Yates algorithm. * @param array an array of double; WILL be modified */ private void shuffleInPlace(double[] array) { lastShuffledState = ((StatefulRandomness)random).getState(); final int n = array.length; System.arraycopy(baseDeck, 0, array, 0, n); // for (int i = 0; i < n; i++) // { // int r = i + LightRNG.determineBounded(lastShuffledState + (i << 1), n - i); // double t = array[r]; // array[r] = array[i]; // array[i] = LightRNG.determineDouble(lastShuffledState + (i << 1) + 1) * 0.0625 + t; // } for (int i = n; i > 1; i--) { final int r = DiverRNG.determineBounded(lastShuffledState + i, i); final double t = array[i - 1]; array[i - 1] = array[r]; array[r] = t; } for (int i = 0; i < n; i++) { array[i] += DiverRNG.determineDouble(lastShuffledState ^ ~i) * 0.0625; } }
/** * Inclusive inner, exclusive outer; lower and upper can be positive or negative and there's no requirement for one * to be greater than or less than the other. * * @param lower the lower bound, inclusive, can be positive or negative * @param upper the upper bound, exclusive, can be positive or negative * @return a random long that may be equal to lower and will otherwise be between lower and upper */ public final long nextLong(final long lower, final long upper) { return lower + nextLong(upper - lower); }
do { switch (DiverRNG.determineBounded(++rngState, 3)) { case 0: evictedKey = key1;
/** * Given a byte array as a parameter, this will fill the array with random bytes (modifying it * in-place). Calls nextLong() {@code Math.ceil(bytes.length / 8.0)} times. * * @param bytes a byte array that will have its contents overwritten with random bytes. */ public final void nextBytes(final byte[] bytes) { int i = bytes.length, n; while (i != 0) { n = Math.min(i, 8); for (long bits = nextLong(); n-- != 0; bits >>>= 8) bytes[--i] = (byte) bits; } }
/** * Default constructor; uses {@link DiverRNG}, which is of high quality, but low period (which rarely matters * for games), and has excellent speed, tiny state size, and natively generates 64-bit numbers. The seed can be * any long, including 0. * @param seed any long */ public RNG(long seed) { this(new DiverRNG(seed)); }
/** * Refills a substance array that can be given to other static methods. * @param substance a 1D double array that will be modified and filled with random values * @param seed the int seed to use for the random contents * @return substance, after modification; should be given to other methods */ public static double[] initializeInto(double[] substance, int seed){ if(substance == null) return null; for (int i = 0; i < substance.length; i++) { substance[i] = (DiverRNG.determine(++seed) >> 11) * fraction; } return substance; }
/** * Makes a CriticalRNG with a luck factor of 0 and a DiverRNG with the given seed for its RandomnessSource. * @param seed any long */ public CriticalRNG(long seed) { super(new DiverRNG(seed)); } /**
/** * Refills a substance array that can be given to other static methods. Uses very little 64-bit math, * making it ideal for GWT. * @param substance a 1D double array that will be modified and filled with random values * @return substance, after modification; should be given to other methods */ public static double[] initializeInto(double[] substance){ if(substance == null) return null; long seed = 65537; for (int i = 0; i < substance.length; i++) { substance[i] = (DiverRNG.determine(++seed) >> 11) * fraction; } return substance; } /**
/** * Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the * copy, both will generate the same sequence of random numbers from the point copy() was called. This just need to * copy the state so it isn't shared, usually, and produce a new value with the same exact state. * * @return a copy of this RandomnessSource */ @Override public DiverRNG copy() { return new DiverRNG(state); }
@Override public double getNoiseWithSeed(final double x, final double y, final double z, final double w, final double u, final double v, long seed) { final long rs = DiverRNG.determine(seed ^ ~seed << 32), rx = 0x9E3779B97F4A7C15L * (rs >>> 23 ^ rs << 23) * (rs | 1L), ry = 0x9E3779B97F4A7C15L * (rx >>> 23 ^ rx << 23) * (rx | 1L), rz = 0x9E3779B97F4A7C15L * (ry >>> 23 ^ ry << 23) * (ry | 1L), rw = 0x9E3779B97F4A7C15L * (rz >>> 23 ^ rz << 23) * (rz | 1L), ru = 0x9E3779B97F4A7C15L * (rw >>> 23 ^ rw << 23) * (rw | 1L), rv = 0x9E3779B97F4A7C15L * (ru >>> 23 ^ ru << 23) * (ru | 1L); return getNoiseWithSeeds(x, y, z, w, u, v, rx, ry, rz, rw, ru, rv); }
/** * Uses the provided source of randomness for all calculations. This constructor should be used if an alternate * RandomnessSource other than DiverRNG is desirable, such as to keep compatibility with earlier SquidLib * versions that defaulted to MersenneTwister, LightRNG, ThrustAltRNG, or LinnormRNG. * <br> * If the parameter is null, this is equivalent to using {@link #RNG()} as the constructor. * @param random the source of pseudo-randomness, such as a LightRNG or LongPeriodRNG object */ public RNG(RandomnessSource random) { this.random = (random == null) ? new DiverRNG() : random; }
@Override public double getNoiseWithSeed(final double x, final double y, final double z, final double w, final long seed) { final long rs = DiverRNG.determine(seed ^ ~seed << 32), rx = 0x9E3779B97F4A7C15L * (rs >>> 23 ^ rs << 23) * (rs | 1L), ry = 0x9E3779B97F4A7C15L * (rx >>> 23 ^ rx << 23) * (rx | 1L), rz = 0x9E3779B97F4A7C15L * (ry >>> 23 ^ ry << 23) * (ry | 1L), rw = 0x9E3779B97F4A7C15L * (rz >>> 23 ^ rz << 23) * (rz | 1L); return getNoiseWithSeeds(x, y, z, w, rx, ry, rz, rw); }
/** * Makes a CriticalRNG with a luck factor of 0 and a randomly-seeded DiverRNG for its RandomnessSource. */ public CriticalRNG() { super(new DiverRNG()); }
scratch = new double[upper]; for (int i = 0; i < len; i++) { seed = DiverRNG.determine(seed + 0xC6BC279692B5CC83L ^ ~seed << 32); scratch3[i * 3 + 1] = (scratch3[i * 3] = (scratch3[i * 3 + 2] = longFloor(coordinates[i])) * seed) + seed;
/** * Makes a CriticalRNG with the given luck factor and a randomly-seeded DiverRNG for its RandomnessSource. * @param luck typically a small float, often between -0.5f and 0.5f, that will affect the results this returns */ public CriticalRNG(float luck) { super(new DiverRNG()); this.luck = luck; }
/** * Makes a CriticalRNG with the given luck factor and a DiverRNG with the given seed for its RandomnessSource. * @param seed any long * @param luck typically a small float, often between -0.5f and 0.5f, that will affect the results this returns */ public CriticalRNG(long seed, float luck) { super(new DiverRNG(seed)); this.luck = luck; }
/** * Default constructor; uses {@link DiverRNG}, which is of high quality, but low period (which rarely matters * for games), and has excellent speed, tiny state size, and natively generates 64-bit numbers. * <br> * Previous versions of SquidLib used different implementations, including {@link LightRNG}, {@link ThrustAltRNG}, * {@link LinnormRNG}, and {@link MersenneTwister}. You can still use one of these by instantiating one of those * classes and passing it to {@link #RNG(RandomnessSource)}, which may be the best way to ensure the same results * across versions. */ public RNG() { this(new DiverRNG()); }