Class ChaosRNG

java.lang.Object
squidpony.squidmath.ChaosRNG
All Implemented Interfaces:
Serializable, RandomnessSource

public class ChaosRNG
extends Object
implements RandomnessSource
An RNG that cannot be seeded and should be fairly hard to predict what it will return next. Useful for competitions where a seeded RNG is used for dungeon generation and enemy placement but an unpredictable RNG is needed for combat, so players can't abuse the RNG to make improbable events guaranteed or unfavorable outcomes impossible.
This is intended to be used as a RandomnessSource for an RNG, and does not have any methods other than those needed for that interface, with one exception -- the randomize() method, which can be used to completely change all (many) bits of state using cryptographic random numbers. If you create a ChaosRNG and keep it around for later, then you can pass it to the RNG constructor and later call randomize() on the ChaosRNG if you suspect it may be becoming predictable. The period on this RNG is preposterously large, since it involves a pair of IsaacRNGs as well as other random state, so predicting it may be essentially impossible unless the user can poke around in the application, use reflection, and so on. Created by Tommy Ettinger on 3/17/2016.
See Also:
Serialized Form
  • Constructor Summary

    Constructors 
    Constructor Description
    ChaosRNG()
    Builds a ChaosRNG with a fairly-random seed derived from somewhat-OK sources of non-seed randomness, such as time before and after garbage collection.
  • Method Summary

    Modifier and Type Method Description
    ChaosRNG copy()
    Produces another ChaosRNG with no relation to this one; this breaks the normal rules that RandomnessSource.copy abides by because this class should never have its generated number sequence be predictable.
    int next​(int bits)
    Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.
    long nextLong()
    Can return any long, positive or negative, of any size permissible in a 64-bit signed integer.
    void randomize()
    Changes the internal state to a new, fully-random version that should have no relation to the previous state.
    String toString()  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ChaosRNG

      public ChaosRNG()
      Builds a ChaosRNG with a fairly-random seed derived from somewhat-OK sources of non-seed randomness, such as time before and after garbage collection. We're forced to use sub-par techniques here due to GWT not supporting any better methods. Future random generation uses less secure methods but should still make it extremely difficult to "divine" the future RNG results from the outputs.
  • Method Details

    • next

      public final int next​(int bits)
      Description copied from interface: RandomnessSource
      Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.
      Specified by:
      next in interface RandomnessSource
      Parameters:
      bits - the number of bits to be returned
      Returns:
      the integer containing the appropriate number of bits
    • nextLong

      public final long nextLong()
      Can return any long, positive or negative, of any size permissible in a 64-bit signed integer.
      Specified by:
      nextLong in interface RandomnessSource
      Returns:
      any long, all 64 bits are random
    • copy

      public ChaosRNG copy()
      Produces another ChaosRNG with no relation to this one; this breaks the normal rules that RandomnessSource.copy abides by because this class should never have its generated number sequence be predictable.
      Specified by:
      copy in interface RandomnessSource
      Returns:
      a new, unrelated ChaosRNG as a RandomnessSource
    • randomize

      public void randomize()
      Changes the internal state to a new, fully-random version that should have no relation to the previous state. May be somewhat slow; you shouldn't need to call this often.
    • toString

      public String toString()
      Overrides:
      toString in class Object