Package com.irurueta.statistics
Class Randomizer
java.lang.Object
com.irurueta.statistics.Randomizer
- Direct Known Subclasses:
GaussianRandomizer
,UniformRandomizer
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
FieldsModifier and TypeFieldDescriptionstatic 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
ConstructorsModifierConstructorDescriptionprotected
Constructor.protected
Randomizer
(Random internalRandom) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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.static Randomizer
create
(boolean useSecureRandom) Creates a new Randomizer instance using DEFAULT_RANDOMIZER_TYPE.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.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
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.abstract RandomizerType
getType()
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
nextInt()
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
nextLong()
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.
-
Field Details
-
USE_SECURE_RANDOM_BY_DEFAULT
public static final boolean USE_SECURE_RANDOM_BY_DEFAULTIndicates that by default non-secured random instance is used if none is provided.- See Also:
-
DEFAULT_RANDOMIZER_TYPE
Indicates default randomizer type if non is provided. By default, uniform randomizers are created. -
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 defaultRandom
implementation. -
Randomizer
Constructor.- Parameters:
internalRandom
- Instance in charge of generating pseudo-random values.- Throws:
NullPointerException
- if provided value is null.
-
-
Method Details
-
getInternalRandom
Returns internal instance in charge of generating pseudo-random values.- Returns:
- instance in charge of generating pseudo-random values.
-
setInternalRandom
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());
- 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
Returns the randomizer type of this instance.- Returns:
- Randomizer type.
-
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
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
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
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
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
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.
-