Package squidpony.squidmath
Class GearRNG
java.lang.Object
squidpony.squidmath.GearRNG
- All Implemented Interfaces:
- Serializable,- RandomnessSource
public final class GearRNG extends Object implements RandomnessSource, Serializable
A larger-period generator with 127 bits of state (two longs, one is always odd), a period of 2 to the 127, and what
 should be slightly better speed than the related OrbitRNG. It passes 32TB of PractRand testing with no anomalies or
 failures. The idea for this generator is to have a simple Weyl sequence (a counter with a large increment) for
 stateA, and at one point to multiply a value based on stateA by stateB, which is always odd. The trick is that stateB
 usually updates by adding a large even number, but about once every 10 billion generated numbers, decided by a
 comparison between a constant and stateA's value, it "shifts gears" and stays on the same value while stateA updates.
 
For some purposes you may want to instead consider
The name comes from shifting gears; pretty straightforward here.
Created by Tommy Ettinger on 3/29/2020.
For some purposes you may want to instead consider
TangleRNG, which also has two states (one odd) and uses a
 very similar algorithm, but it never "shifts gears," which drops its period down to 2 to the 64, makes it a
 SkippingRandomness, and should in theory speed it up (in practice, TangleRNG needs an extra step that actually
 makes it slower than GearRNG). An individual TangleRNG can't produce all possible long outputs and can produce some
 duplicates, but each pair of states for a TangleRNG has a different set of which outputs will be skipped and which
 will be duplicated. Since it would require months of solid number generation to exhaust the period of a TangleRNG, and
 that's the only time an output can be confirmed as skipped, it's probably fine for most usage to use many different
 TangleRNGs, all seeded differently. Other choices: you could use one OrbitRNG (which technically has a longer
 period, but some states produce very similar output), DiverRNG (if you don't mind that it never produces a
 duplicate output), IsaacRNG (if speed is less important but more secure output is), or Lathe64RNG, though all
 of those are probably slower than using one GearRNG object or even many TangleRNG objects.
 The name comes from shifting gears; pretty straightforward here.
Created by Tommy Ettinger on 3/29/2020.
- See Also:
- Serialized Form
- 
Constructor Summary
- 
Method SummaryModifier and Type Method Description GearRNGcopy()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.booleanequals(Object o)longgetStateA()Get the "A" part of the internal state as a long.longgetStateB()Get the "B" part of the internal state as a long.inthashCode()intnext(int bits)Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.longnextLong()Using this method, any algorithm that needs to efficiently generate more than 32 bits of random data can interface with this randomness source.voidsetStateA(long stateA)Set the "A" part of the internal state with a long.voidsetStateB(long stateB)Set the "B" part of the internal state with a long; the lowest bit is always discarded and replaced with 1.StringtoString()
- 
Constructor Details
- 
Method Details- 
getStateAGet the "A" part of the internal state as a long.- Returns:
- the current internal state of this object.
 
- 
setStateASet the "A" part of the internal state with a long.- Parameters:
- stateA- any 64-bit long
 
- 
getStateBGet the "B" part of the internal state as a long.- Returns:
- the current internal "B" state of this object.
 
- 
setStateBSet the "B" part of the internal state with a long; the lowest bit is always discarded and replaced with 1. That is, stateB is always an odd number, and if an even number is given it will be incremented.- Parameters:
- stateB- any 64-bit long
 
- 
nextUsing this method, any algorithm that might use the built-in Java Random can interface with this randomness source.- Specified by:
- nextin interface- RandomnessSource
- Parameters:
- bits- the number of bits to be returned
- Returns:
- the integer containing the appropriate number of bits
 
- 
nextLongUsing this method, any algorithm that needs to efficiently generate more than 32 bits of random data can interface with this randomness source.Get a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive). - Specified by:
- nextLongin interface- RandomnessSource
- Returns:
- a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive)
 
- 
copyProduces 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.- Specified by:
- copyin interface- RandomnessSource
- Returns:
- a copy of this RandomnessSource
 
- 
toString
- 
equals
- 
hashCode
 
-