Package org.flag4j.rng
Class RandomArray
java.lang.Object
org.flag4j.rng.RandomArray
This class contains methods useful for generating arrays filled with random values.
-
Field Summary
Modifier and TypeFieldDescriptionprivate final RandomCNumber
Random number generator to use when creating random arrays. -
Constructor Summary
ConstructorDescriptionCreates a RandomArray object to generate arrays filled with random values using a default random number generator.RandomArray
(RandomCNumber rng) Creates a RandomArray object to generate arrays filled with random values using the specified complex random number generator. -
Method Summary
Modifier and TypeMethodDescriptionCNumber[]
genNormalComplexArray
(int length) Generates an array ofcomplex numbers
with pseudorandom normally distributed magnitudes with a mean of 0.0 and a magnitude of 1.0.CNumber[]
genNormalComplexArray
(int length, double mean, double std) Generates an array ofcomplex numbers
with pseudorandom normally distributed magnitudes with specified mean and standard deviation.double[]
genNormalRealArray
(int length) Generates an array of doubles filled with normally pseudorandom values with a mean of 0 and standard deviation of 1.double[]
genNormalRealArray
(int length, double mean, double std) Generates an array of doubles filled with normally distributed pseudorandom values with a specified mean and standard deviation.private int[]
genRandomRows
(int numIndices, int rowStart, int rowEnd, int maxReps) Helper function to generate random row indices for use inrandomUniqueIndices2D(int, int, int, int, int)
.CNumber[]
genUniformComplexArray
(int length) Generates an array ofcomplex numbers
with pseudorandom uniformly distributed magnitudes in[0.0, 1.0)
.CNumber[]
genUniformComplexArray
(int length, double min, double max) Generates an array of pseudorandomcomplex numbers
with uniformly distributed magnitudes in[min, max)
.double[]
genUniformRealArray
(int length) Generates an array of doubles filled with uniformly distributed pseudorandom values in[0.0, 1.0)
.double[]
genUniformRealArray
(int length, double min, double max) Generates an array of doubles filled with uniformly distributed pseudorandom values in[min, max)
.double[]
genUniformRealIntArray
(int length) Generates an array of integers filled with uniformly distributed pseudorandom int values in [Integer.MAX_VALUE
,Integer.MAX_VALUE
).int[]
genUniformRealIntArray
(int length, int min, int max) Generates an array of integers filled with uniformly distributed pseudorandom values in[min, max)
.int[]
randomUniqueIndices
(int numIndices, int start, int end) Creates unique indices in [start, end).int[][]
randomUniqueIndices2D
(int numIndices, int rowStart, int rowEnd, int colStart, int colEnd) Creates a list of unique two-dimensional indices.void
shuffle
(double[] arr) Randomly shuffles array using the Fisher–Yates algorithm.int[]
shuffle
(int[] arr) Randomly shuffles array using the Fisher–Yates algorithm.void
Randomly shuffles array using the Fisher–Yates algorithm.
-
Field Details
-
rng
Random number generator to use when creating random arrays.
-
-
Constructor Details
-
RandomArray
public RandomArray()Creates a RandomArray object to generate arrays filled with random values using a default random number generator. -
RandomArray
Creates a RandomArray object to generate arrays filled with random values using the specified complex random number generator.- Parameters:
rng
- The complex random number generator to use when creating random arrays.
-
-
Method Details
-
genUniformRealArray
public double[] genUniformRealArray(int length) Generates an array of doubles filled with uniformly distributed pseudorandom values in[0.0, 1.0)
. To generate uniformly distributed values in a specified range seegenUniformRealArray(int, double, double)
.- Parameters:
length
- Length of pseudorandom array to generate.- Returns:
- An array of doubles with specified length filled with uniformly distributed values in
[0.0, 1.0)
.
-
genUniformRealArray
public double[] genUniformRealArray(int length, double min, double max) Generates an array of doubles filled with uniformly distributed pseudorandom values in[min, max)
.- Parameters:
length
- Length of pseudorandom array to generate.min
- Lower bound of uniform range (inclusive).max
- Upper bound of uniform range (Exclusive).- Returns:
- An array of doubles with specified length filled with uniformly distributed values in
[min, max)
.
-
genUniformRealIntArray
public double[] genUniformRealIntArray(int length) Generates an array of integers filled with uniformly distributed pseudorandom int values in [Integer.MAX_VALUE
,Integer.MAX_VALUE
). To generate uniformly distributed values in a specified range seegenUniformRealIntArray(int, int, int)
.- Parameters:
length
- Length of pseudorandom array to generate.- Returns:
- An array of integers with specified length filled with uniformly distributed values in
[
Integer.MAX_VALUE
,Integer.MAX_VALUE
).
-
genUniformRealIntArray
public int[] genUniformRealIntArray(int length, int min, int max) Generates an array of integers filled with uniformly distributed pseudorandom values in[min, max)
.- Parameters:
length
- Length of pseudorandom array to generate.min
- Lower bound of uniform range (inclusive).max
- Upper bound of uniform range (Exclusive).- Returns:
- An array of integers with specified length filled with uniformly distributed values in
[min, max)
.
-
genNormalRealArray
public double[] genNormalRealArray(int length) Generates an array of doubles filled with normally pseudorandom values with a mean of 0 and standard deviation of 1.- Parameters:
length
- Length of pseudorandom array to generate.- Returns:
- An array of doubles with specified length filled with normally pseudorandom values with a mean of 0 and standard deviation of 1.
-
genNormalRealArray
public double[] genNormalRealArray(int length, double mean, double std) Generates an array of doubles filled with normally distributed pseudorandom values with a specified mean and standard deviation.- Parameters:
length
- Length of pseudorandom array to generate.mean
- Mean of normal distribution.std
- Standard deviation of normal distribution.- Returns:
- An array of doubles with specified length filled with normally pseudorandom values specified mean and standard deviation.
- Throws:
IllegalArgumentException
- If standard deviation is negative.
-
genUniformComplexArray
Generates an array ofcomplex numbers
with pseudorandom uniformly distributed magnitudes in[0.0, 1.0)
.- Parameters:
length
- Length of the pseudorandom array to generate.- Returns:
- An array of
complex numbers
with pseudorandom uniformly distributed magnitudes in[0.0, 1.0)
.
-
genUniformComplexArray
Generates an array of pseudorandomcomplex numbers
with uniformly distributed magnitudes in[min, max)
.- Parameters:
length
- Length of the pseudorandom array to generate.min
- Minimum value of uniform distribution from which the magnitudes are sampled (inclusive).max
- Maximum value of uniform distribution from which the magnitudes are sampled (exclusive).- Returns:
- An array of
complex numbers
with pseudorandomcomplex numbers
with uniformly distributed magnitudes in[min, max)
. - Throws:
IllegalArgumentException
- Ifmin
is negative or ifmax
is less thanmin
.
-
genNormalComplexArray
Generates an array ofcomplex numbers
with pseudorandom normally distributed magnitudes with a mean of 0.0 and a magnitude of 1.0.- Parameters:
length
- Length of the pseudorandom array to generate.- Returns:
- An array of
complex numbers
with pseudorandom normally distributed magnitudes with a mean of 0.0 and a magnitude of 1.0.
-
genNormalComplexArray
Generates an array ofcomplex numbers
with pseudorandom normally distributed magnitudes with specified mean and standard deviation.- Parameters:
length
- Length of the pseudorandom array to generate.mean
- Mean of the normal distribution from which to sample magnitudes.std
- Standard deviation of the normal distribution from which to sample magnitudes.- Returns:
- An array of
complex numbers
with pseudorandom normally distributed magnitudes with specified mean and standard deviation. - Throws:
IllegalArgumentException
- If standard deviation is negative.
-
randomUniqueIndices
public int[] randomUniqueIndices(int numIndices, int start, int end) Creates unique indices in [start, end).- Parameters:
numIndices
- Number of random unique indices to get.start
- Staring index (inclusive).end
- Ending index (exclusive).- Returns:
- An array of length
numIndices
containing random unique indices in [start, end). The array will be sorted. - Throws:
IllegalArgumentException
- Ifstart
is not in[0, end)
- See Also:
-
randomUniqueIndices2D
public int[][] randomUniqueIndices2D(int numIndices, int rowStart, int rowEnd, int colStart, int colEnd) Creates a list of unique two-dimensional indices.- Parameters:
numIndices
- Total number of indices to generate.rowStart
- Starting row index (inclusive).rowEnd
- Ending row index (exclusive).colStart
- Starting column index (inclusive).colEnd
- Ending column index (exclusive).- Returns:
- A two-dimensional array of shape
2-by-numIndices
containing unique two-dimensional indices. The first row contains row indices, the second, column indices. The indices will be sorted by rows then columns. - See Also:
-
genRandomRows
private int[] genRandomRows(int numIndices, int rowStart, int rowEnd, int maxReps) Helper function to generate random row indices for use inrandomUniqueIndices2D(int, int, int, int, int)
. This method generates random row indices so that a single row is not repeated more thanmaxReps
times.- Parameters:
numIndices
- Number of indices to generate. Assumed to be less than or equal torowEnd - rowStart
.rowStart
- Minimum row index. Assumed that0 <= rowStart < rowEnd
.rowEnd
- Maximum row index. Assumed that0 <= rowStart < rowEnd
.maxReps
- Maximum number of times a single row index can be repeated.- Returns:
- An array containing random row indices such that no index is repeated more than
maxReps
times. Note: the array is sorted before it is returned.
-
shuffle
public int[] shuffle(int[] arr) Randomly shuffles array using the Fisher–Yates algorithm. This is done in place.- Parameters:
arr
- Array to shuffle.- Returns:
- A reference to
arr
.
-
shuffle
public void shuffle(double[] arr) Randomly shuffles array using the Fisher–Yates algorithm. This is done in place.- Parameters:
arr
- Array to shuffle.
-
shuffle
Randomly shuffles array using the Fisher–Yates algorithm. This is done in place.- Parameters:
arr
- Array to shuffle.
-