Class LowChangeQuasiRandom

java.lang.Object
java.util.Random
com.github.tommyettinger.random.EnhancedRandom
com.github.tommyettinger.random.LowChangeQuasiRandom
All Implemented Interfaces:
Externalizable, Serializable, RandomGenerator

public class LowChangeQuasiRandom extends EnhancedRandom
A quasi-random number generator that only changes one bit in its state per call to nextLong(), and returns that changed state directly. The choice of which bit changes is determined by choice, which itself changes using the same algorithm as GoldenQuasiRandom. That is, choice has 0x9E3779B97F4A7C15L added to it every time a number is generated, and here the top 6 bits are used to choose a bit to change in state.
There's probably some good usage for this, but I don't know what it is yet. This generator may be useful simply because it is so non-random, while still being not-quite-predictable. As an example, it changes from returning a negative number many times in a row, to returning a positive number many times in a row (or vice versa), on approximately 1/64 numbers generated. A fair uniform random number generator would change from negative to positive or vice versa on approximately 1/2 numbers generated.
This has a period of 2 to the 64, and allows all long values for state and for choice. This means there are 2 to the 64 different streams for this generator, though many are probably very similar.
See Also:
  • Field Details

    • state

      public long state
      The primary state of the generator; this is what gets returned by nextLong().
    • choice

      public long choice
      The secondary state of the generator; the upper 6 bits are used to determine which single bit will change in state when a new number is generated.
  • Constructor Details

    • LowChangeQuasiRandom

      public LowChangeQuasiRandom()
    • LowChangeQuasiRandom

      public LowChangeQuasiRandom(long seed)
    • LowChangeQuasiRandom

      public LowChangeQuasiRandom(long state, long choice)
  • Method Details

    • getStateCount

      public int getStateCount()
      Description copied from class: EnhancedRandom
      Gets the number of possible state variables that can be selected with EnhancedRandom.getSelectedState(int) or EnhancedRandom.setSelectedState(int, long). This defaults to returning 0, making no state variable available for reading or writing. An implementation that has only one long state, like DistinctRandom generator, should return 1. A generator that permits setting two different long values, like LaserRandom, should return 2. Much larger values are possible for types like the Mersenne Twister or some CMWC generators.
      Overrides:
      getStateCount in class EnhancedRandom
      Returns:
      the non-negative number of selections possible for state variables
    • getSelectedState

      public long getSelectedState(int selection)
      Description copied from class: EnhancedRandom
      Gets a selected state value from this EnhancedRandom. The number of possible selections is up to the implementing class, and is accessible via EnhancedRandom.getStateCount(), but negative values for selection are typically not tolerated. This should return the exact value of the selected state, assuming it is implemented. The default implementation throws an UnsupportedOperationException, and implementors only have to allow reading the state if they choose to implement this differently. If this method is intended to be used, EnhancedRandom.getStateCount() must also be implemented.
      Overrides:
      getSelectedState in class EnhancedRandom
      Parameters:
      selection - used to select which state variable to get; generally non-negative
      Returns:
      the exact value of the selected state
    • setSelectedState

      public void setSelectedState(int selection, long value)
      Description copied from class: EnhancedRandom
      Sets a selected state value to the given long value. The number of possible selections is up to the implementing class, but negative values for selection are typically not tolerated. Implementors are permitted to change value if it is not valid, but they should not alter it if it is valid. The public implementation calls EnhancedRandom.setSeed(long) with value, which doesn't need changing if the generator has one state that is set verbatim by setSeed(). Otherwise, this method should be implemented when EnhancedRandom.getSelectedState(int) is and the state is allowed to be set by users. Having accurate ways to get and set the full state of a random number generator makes it much easier to serialize and deserialize that class.
      Overrides:
      setSelectedState in class EnhancedRandom
      Parameters:
      selection - used to select which state variable to set; generally non-negative
      value - the exact value to use for the selected state, if valid
    • setState

      public void setState(long state)
      Description copied from class: EnhancedRandom
      Sets each state variable to the given state. If EnhancedRandom.getStateCount() is 1, then this should set the whole state to the given value using EnhancedRandom.setSelectedState(int, long). If getStateCount() is more than 1, then all states will be set in the same way (using setSelectedState(), all to state).
      Overrides:
      setState in class EnhancedRandom
      Parameters:
      state - the long value to use for each state variable
    • setState

      public void setState(long stateA, long stateB)
      Description copied from class: EnhancedRandom
      Sets each state variable to either stateA or stateB, alternating. This uses EnhancedRandom.setSelectedState(int, long) to set the values. If there is one state variable (EnhancedRandom.getStateCount() is 1), then this only sets that state variable to stateA. If there are two state variables, the first is set to stateA, and the second to stateB. If there are more, it reuses stateA, then stateB, then stateA, and so on until all variables are set.
      Overrides:
      setState in class EnhancedRandom
      Parameters:
      stateA - the long value to use for states at index 0, 2, 4, 6...
      stateB - the long value to use for states at index 1, 3, 5, 7...
    • getTag

      public String getTag()
      Description copied from class: EnhancedRandom
      Gets the tag used to identify this type of EnhancedRandom, as a String. This tag should be unique, and for uniformity purposes, all tags used in this library are 4 characters long. User-defined tags should have a different length.
      Specified by:
      getTag in class EnhancedRandom
      Returns:
      a unique String identifier for this type of EnhancedRandom; usually 4 chars long.
    • setSeed

      public void setSeed(long seed)
      Description copied from class: EnhancedRandom
      Sets the seed of this random number generator using a single long seed. This should behave exactly the same as if a new object of this type was created with the constructor that takes a single long value. This does not necessarily assign the state variable(s) of the implementation with the exact contents of seed, so EnhancedRandom.getSelectedState(int) should not be expected to return seed after this, though it may. If this implementation has more than one long of state, then the expectation is that none of those state variables will be exactly equal to seed (almost all the time).
      Specified by:
      setSeed in class EnhancedRandom
      Parameters:
      seed - the initial seed
    • nextLong

      public long nextLong()
      Description copied from class: EnhancedRandom
      Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. The general contract of nextLong is that one long value is pseudorandomly generated and returned.
      The only methods that need to be implemented by this interface are this and EnhancedRandom.copy(), though other methods can be implemented as appropriate for generators that, for instance, natively produce ints rather than longs.
      Specified by:
      nextLong in interface RandomGenerator
      Specified by:
      nextLong in class EnhancedRandom
      Returns:
      the next pseudorandom, uniformly distributed long value from this random number generator's sequence
    • previousLong

      public long previousLong()
      Description copied from class: EnhancedRandom
      Optional; moves the state to its previous value and returns the previous long that would have been produced by EnhancedRandom.nextLong(). This can be equivalent to calling EnhancedRandom.skip(long) with -1L, but not always; many generators can't efficiently skip long distances, but can step back by one value.
      Generators that natively generate int results typically produce long values by generating an int for the high 32 bits and an int for the low 32 bits. When producing the previous long, the order the high and low bits are generated, such as by EnhancedRandom.previousInt(), should be reversed. Generators that natively produce long values usually don't need to implement EnhancedRandom.previousInt(), but those that produce int usually should implement it, and may optionally call previousInt() twice in this method.
      If you know how to implement the reverse of a particular random number generator, it is recommended you do so here, rather than rely on skip(). This isn't always easy, but should always be possible for any decent PRNG (some historical PRNGs, such as the Middle-Square PRNG, cannot be reversed at all). If a generator cannot be reversed because multiple initial states can transition to the same subsequent state, it is known to have statistical problems that are not necessarily present in a generator that matches one initial state to one subsequent state.
      The public implementation calls EnhancedRandom.skip(long) with -1L, and if skip() has not been implemented differently, then it will throw an UnsupportedOperationException.
      Overrides:
      previousLong in class EnhancedRandom
      Returns:
      the previous number this would have produced with EnhancedRandom.nextLong()
    • copy

      public LowChangeQuasiRandom copy()
      Description copied from class: EnhancedRandom
      Creates a new EnhancedRandom with identical states to this one, so if the same EnhancedRandom methods are called on this object and its copy (in the same order), the same outputs will be produced. This is not guaranteed to copy the inherited state of any parent class, so if you call methods that are only implemented by a superclass (like Random) and not this one, the results may differ.
      Specified by:
      copy in class EnhancedRandom
      Returns:
      a deep copy of this EnhancedRandom.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object