Class Randomizer

java.lang.Object
com.irurueta.statistics.Randomizer
Direct Known Subclasses:
GaussianRandomizer, UniformRandomizer

public abstract class Randomizer extends Object
Parent class of all Randomizers. Specific subclasses exist for different distribution types (currently only uniform and gaussian distributions are implemented). This class provides static methods to ease the instantiation of a Randomizer. For instance, a UniformRandomizer can be created like this:
 
 Randomizer.create(RandomizerType.UNIFORM_RANDOMIZER);
 
 
whereas a GaussianRandomizer can be created like this:
 
 Randomizer.create(RandomizerType.GAUSSIAN_RANDOMIZER);
 
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final RandomizerType
    Indicates default randomizer type if non is provided.
    private Random
    Instance in charge of generating pseudo-random values.
    static final boolean
    Indicates that by default non-secured random instance is used if none is provided.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor.
    protected
    Randomizer(Random internalRandom)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Randomizer
    Creates a new Randomizer instance using DEFAULT_RANDOMIZER_TYPE and USE_SECURE_RANDOM_By_DEFAULT to determine what type of random distribution is to be used and whether the random generator must be secure or not.
    static Randomizer
    create(boolean useSecureRandom)
    Creates a new Randomizer instance using DEFAULT_RANDOMIZER_TYPE.
    static Randomizer
    Creates a new Randomizer instance using provided randomizer type and the default secure mode specified by USE_SECURE_RANDOM_BY_DEFAULT.
    static Randomizer
    create(RandomizerType type, boolean useSecureRandom)
    Creates a new Randomizer instance using provided randomizer type and provided secure mode.
    static Randomizer
    create(RandomizerType type, Random internalRandom)
    Creates a new Randomizer instance using provided randomizer type and internal randomizer.
    static Randomizer
    create(Random internalRandomizer)
    Creates a new Randomizer instance using provided internal randomizer instance.
    void
    fill(boolean[] array)
    Fills provided array with random booleans.
    void
    fill(double[] array)
    Fills provided array with random double precision floating point values.
    void
    fill(float[] array)
    Fills provided array with random floating point values.
    void
    fill(int[] array)
    Fills provided array with random integer values.
    void
    fill(long[] array)
    Fills provided array with random long values.
    Returns internal instance in charge of generating pseudo-random values.
    Returns the randomizer type of this instance.
    abstract boolean
    Returns next random boolean value following a given distribution depending on the randomizer type.
    boolean[]
    nextBooleans(int length)
    Returns array of booleans.
    abstract double
    Returns next random double precision floating-point value following a given distribution depending on the randomizer type.
    double[]
    nextDoubles(int length)
    Returns array of double precision floating point values.
    abstract float
    Returns next random floating-point value following a given distribution depending on the randomizer type.
    float[]
    nextFloats(int length)
    Returns array of floating point values.
    abstract int
    Returns next random integer value following a given distribution depending on the randomizer type.
    int[]
    nextInts(int length)
    Returns array of random integers.
    abstract long
    Returns next random long value following a given distribution depending on the randomizer type.
    long[]
    nextLongs(int length)
    Returns array of random long values.
    void
    setInternalRandom(Random internalRandom)
    Sets internal instance in charge of generating pseudo-random values.
    void
    setSeed(long seed)
    Sets the seed for the internal randomizer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • USE_SECURE_RANDOM_BY_DEFAULT

      public static final boolean USE_SECURE_RANDOM_BY_DEFAULT
      Indicates that by default non-secured random instance is used if none is provided.
      See Also:
    • DEFAULT_RANDOMIZER_TYPE

      public static final RandomizerType DEFAULT_RANDOMIZER_TYPE
      Indicates default randomizer type if non is provided. By default, uniform randomizers are created.
    • internalRandom

      private Random internalRandom
      Instance in charge of generating pseudo-random values. Secure instances can be used if the generated values need to be ensured to be "more" random at the expense of higher computational cost.
  • Constructor Details

    • Randomizer

      protected Randomizer()
      Constructor. Uses default Random implementation.
    • Randomizer

      protected Randomizer(Random internalRandom)
      Constructor.
      Parameters:
      internalRandom - Instance in charge of generating pseudo-random values.
      Throws:
      NullPointerException - if provided value is null.
  • Method Details

    • getInternalRandom

      public Random getInternalRandom()
      Returns internal instance in charge of generating pseudo-random values.
      Returns:
      instance in charge of generating pseudo-random values.
    • setInternalRandom

      public void setInternalRandom(Random internalRandom)
      Sets internal instance in charge of generating pseudo-random values.
      Parameters:
      internalRandom - Instance in charge of generating pseudo-random values.
      Throws:
      NullPointerException - if provided value is null.
    • setSeed

      public void setSeed(long seed)
      Sets the seed for the internal randomizer. A seed determines the sequence of pseudo-random numbers to be generated. Whenever the same seed is used on different executions, the generated sequence will be the same. To alleviate this issue and allow the generation of "more" random values the system clock can be used as the seed. Example:
       
       Randomizer randomizer = Randomizer.create();
       randomizer.setSeed(System.currentTimeMillis());
       
       
      Notice that using the clock as seed is not a secure solution to generate random values. If a secure solution is required (i.e. for encryption purposes, then a SecureRandom instance must be provided as the internal random instance)
      Parameters:
      seed - Value to be used as seed
    • nextBoolean

      public abstract boolean nextBoolean()
      Returns next random boolean value following a given distribution depending on the randomizer type.
      Returns:
      Next random boolean value
    • fill

      public void fill(boolean[] array)
      Fills provided array with random booleans.
      Parameters:
      array - Array to be filled.
    • nextBooleans

      public boolean[] nextBooleans(int length)
      Returns array of booleans.
      Parameters:
      length - Length of array to be returned.
      Returns:
      Array of uniform booleans.
      Throws:
      IllegalArgumentException - if provided value is zero or negative.
    • nextInt

      public abstract int nextInt()
      Returns next random integer value following a given distribution depending on the randomizer type.
      Returns:
      Next random integer value.
    • fill

      public void fill(int[] array)
      Fills provided array with random integer values.
      Parameters:
      array - Array to be filled.
    • nextInts

      public int[] nextInts(int length)
      Returns array of random integers.
      Parameters:
      length - Length of array to be returned.
      Returns:
      Array of random integers.
      Throws:
      IllegalArgumentException - if provided value is zero or negative.
    • nextLong

      public abstract long nextLong()
      Returns next random long value following a given distribution depending on the randomizer type.
      Returns:
      Next random integer value.
    • fill

      public void fill(long[] array)
      Fills provided array with random long values.
      Parameters:
      array - Array to be filled.
    • nextLongs

      public long[] nextLongs(int length)
      Returns array of random long values.
      Parameters:
      length - Length of array to be returned.
      Returns:
      Array of random long values.
      Throws:
      IllegalArgumentException - if provided value is zero or negative.
    • nextFloat

      public abstract float nextFloat()
      Returns next random floating-point value following a given distribution depending on the randomizer type.
      Returns:
      Next random integer value.
    • fill

      public void fill(float[] array)
      Fills provided array with random floating point values.
      Parameters:
      array - Array to be filled.
    • nextFloats

      public float[] nextFloats(int length)
      Returns array of floating point values.
      Parameters:
      length - Length of array to be returned.
      Returns:
      Array of random float values.
      Throws:
      IllegalArgumentException - if provided value is zero or negative.
    • nextDouble

      public abstract double nextDouble()
      Returns next random double precision floating-point value following a given distribution depending on the randomizer type.
      Returns:
      Next random double precision floating-point value.
    • fill

      public void fill(double[] array)
      Fills provided array with random double precision floating point values.
      Parameters:
      array - Array to be filled.
    • nextDoubles

      public double[] nextDoubles(int length)
      Returns array of double precision floating point values.
      Parameters:
      length - Length of array to be returned.
      Returns:
      Array of random double values.
      Throws:
      IllegalArgumentException - if provided value is zero or negative.
    • getType

      public abstract RandomizerType getType()
      Returns the randomizer type of this instance.
      Returns:
      Randomizer type.
    • create

      public static Randomizer create()
      Creates a new Randomizer instance using DEFAULT_RANDOMIZER_TYPE and USE_SECURE_RANDOM_By_DEFAULT to determine what type of random distribution is to be used and whether the random generator must be secure or not.
      Returns:
      A Randomizer instance using default randomizer type and secure mode.
    • create

      public static Randomizer create(boolean useSecureRandom)
      Creates a new Randomizer instance using DEFAULT_RANDOMIZER_TYPE. The instantiated Randomizer will generate secure values depending on provided parameter. When secure random generator is requested, generated values tend to be more "random" at the expense of larger computational cost. Secure mode should only be used on specific applications, such as cryptography, and hence the name of the parameter.
      Parameters:
      useSecureRandom - Parameter indicating whether generated values will be used for security purposes such as cryptography.
      Returns:
      A Randomizer instance using provided secure mode.
    • create

      public static Randomizer create(Random internalRandomizer)
      Creates a new Randomizer instance using provided internal randomizer instance.
      Parameters:
      internalRandomizer - Internal instance to be used for generation of pseudo-random values.
      Returns:
      A Randomizer instance using provided internal randomizer.
      Throws:
      NullPointerException - Exception thrown if provided internal randomizer is null.
    • create

      public static Randomizer create(RandomizerType type)
      Creates a new Randomizer instance using provided randomizer type and the default secure mode specified by USE_SECURE_RANDOM_BY_DEFAULT.
      Parameters:
      type - Randomizer type to be used when creating an instance.
      Returns:
      A Randomizer instance using provided randomizer type.
    • create

      public static Randomizer create(RandomizerType type, boolean useSecureRandom)
      Creates a new Randomizer instance using provided randomizer type and provided secure mode.
      Parameters:
      type - Randomizer type to be used when creating an instance.
      useSecureRandom - Boolean indicating whether generated values will be secure for specific purposes such as cryptography. Secure mode generates more "random" values at the expense of higher computational cost.
      Returns:
      A Randomizer instance using provided randomizer type and secure mode.
    • create

      public static Randomizer create(RandomizerType type, Random internalRandom)
      Creates a new Randomizer instance using provided randomizer type and internal randomizer.
      Parameters:
      type - Randomizer type to be used when creating an instance.
      internalRandom - Internal random instance to be used to generate pseudo-random values.
      Returns:
      A Randomizer instance using provided randomizer type and internal random instance.
      Throws:
      NullPointerException - Exception thrown if internal random is null.