Class DistributedRNG

java.lang.Object
squidpony.squidmath.AbstractRNG
squidpony.squidmath.DistributedRNG
All Implemented Interfaces:
Serializable, IRNG, IStatefulRNG, RandomnessSource, StatefulRandomness

public class DistributedRNG
extends AbstractRNG
implements IStatefulRNG, StatefulRandomness, Serializable
An implementation of IRNG that allows specifying a distribution for all random numbers it produces via a IDistribution.SimpleDistribution value. You can adapt any IDistribution to a SimpleDistribution with the static methods in SimpleDistribution, like IDistribution.SimpleDistribution.fractionalDistribution(IDistribution). If no distribution is specified, this uses CurvedBoundedDistribution.instance.
This uses a MoonwalkRNG internally to handle the number generation that the distribution requests. While you can call methods on rng that are specific to MoonwalkRNG, many distributions will get multiple random numbers where a normal RNG would only get one, and this makes the state-jumping features of MoonwalkRNG less useful here. It's still a fast generator when it comes to generating doubles, which is why it's used here; GWT-oriented generators would be slower at generating doubles on desktop and many mobile platforms.
Created by Tommy Ettinger on 11/27/2019.
See Also:
Serialized Form
  • Field Details

  • Constructor Details

  • Method Details

    • next

      public final int next​(int bits)
      Get up to 32 bits (inclusive) of random output; the int this produces will not require more than bits bits to represent.
      Specified by:
      next in interface IRNG
      Specified by:
      next in interface RandomnessSource
      Specified by:
      next in class AbstractRNG
      Parameters:
      bits - an int between 1 and 32, both inclusive
      Returns:
      a random number that fits in the specified number of bits
    • nextInt

      public int nextInt()
      Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).
      Specified by:
      nextInt in interface IRNG
      Specified by:
      nextInt in class AbstractRNG
      Returns:
      a 32-bit random int.
    • nextLong

      public long nextLong()
      Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive). This implementation has "holes" in its range, numbers that it cannot produce regardless of distribution.
      Specified by:
      nextLong in interface IRNG
      Specified by:
      nextLong in interface RandomnessSource
      Specified by:
      nextLong in class AbstractRNG
      Returns:
      a 64-bit random long.
    • nextBoolean

      public boolean nextBoolean()
      Get a random bit of state, interpreted as true or false with approximately equal likelihood.
      Specified by:
      nextBoolean in interface IRNG
      Specified by:
      nextBoolean in class AbstractRNG
      Returns:
      a random boolean.
    • nextDouble

      public double nextDouble()
      Gets a random double between 0.0 inclusive and 1.0 exclusive. This returns a maximum of 0.9999999999999999 because that is the largest double value that is less than 1.0 .
      Specified by:
      nextDouble in interface IRNG
      Specified by:
      nextDouble in class AbstractRNG
      Returns:
      a double between 0.0 (inclusive) and 0.9999999999999999 (inclusive)
    • nextFloat

      public float nextFloat()
      Gets a random float between 0.0f inclusive and 1.0f exclusive. This returns a maximum of 0.99999994 because that is the largest float value that is less than 1.0f .
      Specified by:
      nextFloat in interface IRNG
      Specified by:
      nextFloat in class AbstractRNG
      Returns:
      a float between 0f (inclusive) and 0.99999994f (inclusive)
    • nextInt

      public int nextInt​(int bound)
      Returns a random non-negative integer below the given bound, or 0 if the bound is 0 or negative.
      Specified by:
      nextInt in interface IRNG
      Overrides:
      nextInt in class AbstractRNG
      Parameters:
      bound - the upper bound (exclusive)
      Returns:
      the found number
    • nextLong

      public long nextLong​(long bound)
      Exclusive on bound (which must be positive), with an inner bound of 0. If bound is negative or 0 this always returns 0.
      Specified by:
      nextLong in interface IRNG
      Overrides:
      nextLong in class AbstractRNG
      Parameters:
      bound - the outer exclusive bound; should be positive, otherwise this always returns 0L
      Returns:
      a random long between 0 (inclusive) and bound (exclusive)
    • nextSignedLong

      public long nextSignedLong​(long bound)
      Exclusive on bound (which may be positive or negative), with an inner bound of 0. If bound is negative this returns a negative long; if bound is positive this returns a positive long. The bound can even be 0, which will cause this to return 0L every time.
      Specified by:
      nextSignedLong in interface IRNG
      Overrides:
      nextSignedLong in class AbstractRNG
      Parameters:
      bound - the outer exclusive bound; can be positive or negative
      Returns:
      a random long between 0 (inclusive) and bound (exclusive)
    • nextSignedInt

      public int nextSignedInt​(int bound)
      Returns a random non-negative integer between 0 (inclusive) and the given bound (exclusive), or 0 if the bound is 0. The bound can be negative, which will produce 0 or a negative result.
      Specified by:
      nextSignedInt in interface IRNG
      Overrides:
      nextSignedInt in class AbstractRNG
      Parameters:
      bound - the outer bound (exclusive), can be negative or positive
      Returns:
      the found number
    • copy

      public DistributedRNG copy()
      Creates a copy of this IRNG; it will generate the same random numbers, given the same calls in order, as this IRNG at the point copy() is called. The copy will not share references with this IRNG, except to distribution, which usually shouldn't change much.
      Specified by:
      copy in interface IRNG
      Specified by:
      copy in interface RandomnessSource
      Specified by:
      copy in interface StatefulRandomness
      Specified by:
      copy in class AbstractRNG
      Returns:
      a copy of this IRNG
    • toSerializable

      Gets a view of this IRNG in a way that implements Serializable, which may simply be this IRNG if it implements Serializable as well as IRNG.
      Specified by:
      toSerializable in interface IRNG
      Specified by:
      toSerializable in class AbstractRNG
      Returns:
      a Serializable view of this IRNG or a similar one; may be this
    • getState

      public long getState()
      Get the current internal state of the StatefulRandomness as a long.
      Specified by:
      getState in interface StatefulRandomness
      Returns:
      the current internal state of this object.
    • setState

      public void setState​(long state)
      Set the current internal state of this StatefulRandomness with a long; this accepts a state of 0 with no issues.
      Specified by:
      setState in interface StatefulRandomness
      Parameters:
      state - a 64-bit long.