Class RandomDataGenerator
- java.lang.Object
-
- org.hipparchus.random.RandomDataGenerator
-
- All Implemented Interfaces:
Serializable,RandomGenerator
public class RandomDataGenerator extends Object implements RandomGenerator, Serializable
A class for generating random data.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description RandomDataGenerator()Construct a RandomDataGenerator with a default RandomGenerator as its source of random data.RandomDataGenerator(long seed)Construct a RandomDataGenerator with a default RandomGenerator as its source of random data, initialized with the given seed value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected RandomGeneratordelegate()Returns the backing delegate instance that methods are forwarded to.doublenextBeta(double alpha, double beta)Returns the next pseudo-random beta-distributed value with the given shape and scale parameters.booleannextBoolean()Returns the next pseudorandom, uniformly distributedbooleanvalue from this random number generator's sequence.voidnextBytes(byte[] bytes)Generates random bytes and places them into a user-supplied byte array.voidnextBytes(byte[] bytes, int offset, int length)Generates random bytes and places them into a user-supplied byte array.intnextDeviate(IntegerDistribution dist)Returns a random deviate from the given distribution.doublenextDeviate(RealDistribution dist)Returns a random deviate from the given distribution.int[]nextDeviates(IntegerDistribution dist, int size)Returns an array of random deviates from the given distribution.double[]nextDeviates(RealDistribution dist, int size)Returns an array of random deviates from the given distribution.doublenextDouble()Returns the next pseudorandom, uniformly distributeddoublevalue between0.0and1.0from this random number generator's sequence.doublenextExponential(double mean)Returns the next pseudo-random, exponentially distributed deviate.floatnextFloat()Returns the next pseudorandom, uniformly distributedfloatvalue between0.0and1.0from this random number generator's sequence.doublenextGamma(double shape, double scale)Returns the next pseudo-random gamma-distributed value with the given shape and scale parameters.doublenextGaussian()Returns the next pseudorandom, Gaussian ("normally") distributeddoublevalue with mean0.0and standard deviation1.0from this random number generator's sequence.StringnextHexString(int len)Generates a random string of hex characters of lengthlen.intnextInt()Returns the next pseudorandom, uniformly distributedintvalue from this random number generator's sequence.intnextInt(int n)Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.intnextInt(int lower, int upper)Returns a uniformly distributed random integer between lower and upper (inclusive).doublenextLogNormal(double shape, double scale)Returns the next log-normally-distributed pseudo-random deviate.longnextLong()Returns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence.longnextLong(long n)Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.longnextLong(long lower, long upper)Returns a uniformly distributed random long integer between lower and upper (inclusive).doublenextNormal(double mean, double standardDeviation)Returns the next normally-distributed pseudo-random deviate.int[]nextPermutation(int n, int k)Generates an integer array of lengthkwhose entries are selected randomly, without repetition, from the integers0, ..., n - 1(inclusive).intnextPoisson(double mean)Returns a poisson-distributed deviate with the given mean.double[]nextSample(double[] a, int k)Returns an array ofkdouble values selected randomly from the double arraya.Object[]nextSample(Collection<?> c, int k)Returns an array ofkobjects selected randomly from the Collectionc.int[]nextSampleWithReplacement(int sampleSize, double[] weights)Generates a random sample of size sampleSize from {0, 1, ... , weights.length - 1}, using weights as probabilities.doublenextUniform(double lower, double upper)Returns a double value uniformly distributed over [lower, upper]intnextZipf(int numberOfElements, double exponent)Returns an integer value following a Zipf distribution with the given parameter.static RandomDataGeneratorof(RandomGenerator randomGenerator)Factory method to create aRandomDatainstance using the suppliedRandomGenerator.voidsetSeed(int seed)Sets the seed of the underlying random number generator using anintseed.voidsetSeed(int[] seed)Sets the seed of the underlying random number generator using anintarray seed.voidsetSeed(long seed)Sets the seed of the underlying random number generator using alongseed.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.hipparchus.random.RandomGenerator
nextBoolean, nextBytes, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong, nextLong, setSeed, setSeed, setSeed
-
-
-
-
Constructor Detail
-
RandomDataGenerator
public RandomDataGenerator()
Construct a RandomDataGenerator with a default RandomGenerator as its source of random data.
-
RandomDataGenerator
public RandomDataGenerator(long seed)
Construct a RandomDataGenerator with a default RandomGenerator as its source of random data, initialized with the given seed value.- Parameters:
seed- seed value
-
-
Method Detail
-
of
public static RandomDataGenerator of(RandomGenerator randomGenerator)
Factory method to create aRandomDatainstance using the suppliedRandomGenerator.- Parameters:
randomGenerator- source of random bits- Returns:
- a RandomData using the given RandomGenerator to source bits
- Throws:
MathIllegalArgumentException- if randomGenerator is null
-
delegate
protected RandomGenerator delegate()
Returns the backing delegate instance that methods are forwarded to.Concrete subclasses override this method to supply the instance being decorated.
- Returns:
- the delegate instance
-
nextBeta
public double nextBeta(double alpha, double beta)Returns the next pseudo-random beta-distributed value with the given shape and scale parameters.- Parameters:
alpha- First shape parameter (must be positive).beta- Second shape parameter (must be positive).- Returns:
- beta-distributed random deviate
-
nextExponential
public double nextExponential(double mean)
Returns the next pseudo-random, exponentially distributed deviate.- Parameters:
mean- mean of the exponential distribution- Returns:
- exponentially distributed deviate about the given mean
-
nextGamma
public double nextGamma(double shape, double scale)Returns the next pseudo-random gamma-distributed value with the given shape and scale parameters.- Parameters:
shape- shape parameter of the distributionscale- scale parameter of the distribution- Returns:
- gamma-distributed random deviate
-
nextNormal
public double nextNormal(double mean, double standardDeviation)Returns the next normally-distributed pseudo-random deviate.- Parameters:
mean- mean of the normal distributionstandardDeviation- standard deviation of the normal distribution- Returns:
- a random value, normally distributed with the given mean and standard deviation
-
nextLogNormal
public double nextLogNormal(double shape, double scale)Returns the next log-normally-distributed pseudo-random deviate.- Parameters:
shape- shape parameter of the log-normal distributionscale- scale parameter of the log-normal distribution- Returns:
- a random value, normally distributed with the given mean and standard deviation
-
nextPoisson
public int nextPoisson(double mean)
Returns a poisson-distributed deviate with the given mean.- Parameters:
mean- expected value- Returns:
- poisson deviate
- Throws:
MathIllegalArgumentException- if mean is not strictly positive
-
nextDeviate
public double nextDeviate(RealDistribution dist)
Returns a random deviate from the given distribution.- Parameters:
dist- the distribution to sample from- Returns:
- a random value following the given distribution
-
nextDeviates
public double[] nextDeviates(RealDistribution dist, int size)
Returns an array of random deviates from the given distribution.- Parameters:
dist- the distribution to sample fromsize- the number of values to return- Returns:
- an array of
sizevalues following the given distribution
-
nextDeviate
public int nextDeviate(IntegerDistribution dist)
Returns a random deviate from the given distribution.- Parameters:
dist- the distribution to sample from- Returns:
- a random value following the given distribution
-
nextDeviates
public int[] nextDeviates(IntegerDistribution dist, int size)
Returns an array of random deviates from the given distribution.- Parameters:
dist- the distribution to sample fromsize- the number of values to return- Returns:
- an array of
sizevalues following the given distribution
-
nextInt
public int nextInt(int lower, int upper)Returns a uniformly distributed random integer between lower and upper (inclusive).- Parameters:
lower- lower bound for the generated valueupper- upper bound for the generated value- Returns:
- a random integer value within the given bounds
- Throws:
MathIllegalArgumentException- if lower is not strictly less than or equal to upper
-
nextLong
public long nextLong(long lower, long upper) throws MathIllegalArgumentExceptionReturns a uniformly distributed random long integer between lower and upper (inclusive).- Parameters:
lower- lower bound for the generated valueupper- upper bound for the generated value- Returns:
- a random long integer value within the given bounds
- Throws:
MathIllegalArgumentException- if lower is not strictly less than or equal to upper
-
nextUniform
public double nextUniform(double lower, double upper)Returns a double value uniformly distributed over [lower, upper]- Parameters:
lower- lower boundupper- upper bound- Returns:
- uniform deviate
- Throws:
MathIllegalArgumentException- if upper is less than or equal to upper
-
nextZipf
public int nextZipf(int numberOfElements, double exponent)Returns an integer value following a Zipf distribution with the given parameter.- Parameters:
numberOfElements- number of elements of the distributionexponent- exponent of the distribution- Returns:
- random Zipf value
-
nextHexString
public String nextHexString(int len) throws MathIllegalArgumentException
Generates a random string of hex characters of lengthlen.The generated string will be random, but not cryptographically secure.
Algorithm Description: hex strings are generated using a 2-step process.
len / 2 + 1binary bytes are generated using the underlying Random- Each binary byte is translated into 2 hex digits
- Parameters:
len- the desired string length.- Returns:
- the random string.
- Throws:
MathIllegalArgumentException- iflen <= 0.
-
nextPermutation
public int[] nextPermutation(int n, int k) throws MathIllegalArgumentExceptionGenerates an integer array of lengthkwhose entries are selected randomly, without repetition, from the integers0, ..., n - 1(inclusive).Generated arrays represent permutations of
This method callsntakenkat a time.MathArrays.shufflein order to create a random shuffle of the set of natural numbers{ 0, 1, ..., n - 1 }.- Parameters:
n- the domain of the permutationk- the size of the permutation- Returns:
- a random
k-permutation ofn, as an array of integers - Throws:
MathIllegalArgumentException- ifk > n.MathIllegalArgumentException- ifk <= 0.
-
nextSample
public Object[] nextSample(Collection<?> c, int k) throws MathIllegalArgumentException
Returns an array ofkobjects selected randomly from the Collectionc.Sampling from
cis without replacement; but ifccontains identical objects, the sample may include repeats. If all elements ofcare distinct, the resulting object array represents a Simple Random Sample of sizekfrom the elements ofc.This method calls
nextPermutation(c.size(), k)in order to sample the collection.- Parameters:
c- the collection to be sampledk- the size of the sample- Returns:
- a random sample of
kelements fromc - Throws:
MathIllegalArgumentException- ifk > c.size().MathIllegalArgumentException- ifk <= 0.
-
nextSample
public double[] nextSample(double[] a, int k) throws MathIllegalArgumentExceptionReturns an array ofkdouble values selected randomly from the double arraya.Sampling from
ais without replacement; but ifacontains identical objects, the sample may include repeats. If all elements ofaare distinct, the resulting object array represents a Simple Random Sample of sizekfrom the elements ofa.- Parameters:
a- the array to be sampledk- the size of the sample- Returns:
- a random sample of
kelements froma - Throws:
MathIllegalArgumentException- ifk > c.size().MathIllegalArgumentException- ifk <= 0.
-
nextSampleWithReplacement
public int[] nextSampleWithReplacement(int sampleSize, double[] weights)Generates a random sample of size sampleSize from {0, 1, ... , weights.length - 1}, using weights as probabilities.For 0 < i < weights.length, the probability that i is selected (on any draw) is weights[i]. If necessary, the weights array is normalized to sum to 1 so that weights[i] is a probability and the array sums to 1.
Weights can be 0, but must not be negative, infinite or NaN. At least one weight must be positive.
- Parameters:
sampleSize- size of sample to generateweights- probability sampling weights- Returns:
- an array of integers between 0 and weights.length - 1
- Throws:
MathIllegalArgumentException- if weights contains negative, NaN or infinite values or only 0s or sampleSize is less than 0
-
setSeed
public void setSeed(int seed)
Sets the seed of the underlying random number generator using anintseed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeedin interfaceRandomGenerator- Parameters:
seed- the seed value
-
setSeed
public void setSeed(int[] seed)
Sets the seed of the underlying random number generator using anintarray seed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeedin interfaceRandomGenerator- Parameters:
seed- the seed value
-
setSeed
public void setSeed(long seed)
Sets the seed of the underlying random number generator using alongseed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeedin interfaceRandomGenerator- Parameters:
seed- the seed value
-
nextBytes
public void nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.- Specified by:
nextBytesin interfaceRandomGenerator- Parameters:
bytes- the non-null byte array in which to put the random bytes
-
nextBytes
public void nextBytes(byte[] bytes, int offset, int length)Generates random bytes and places them into a user-supplied byte array.- Specified by:
nextBytesin interfaceRandomGenerator- Parameters:
bytes- the non-null byte array in which to put the random bytesoffset- the starting index for inserting the generated bytes into the arraylength- the number of bytes to generate
-
nextInt
public int nextInt()
Returns the next pseudorandom, uniformly distributedintvalue from this random number generator's sequence.All 232 possible
intvalues should be produced with (approximately) equal probability.- Specified by:
nextIntin interfaceRandomGenerator- Returns:
- the next pseudorandom, uniformly distributed
intvalue from this random number generator's sequence
-
nextInt
public int nextInt(int n)
Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.- Specified by:
nextIntin interfaceRandomGenerator- Parameters:
n- the bound on the random number to be returned. Must be positive.- Returns:
- a pseudorandom, uniformly distributed
intvalue between 0 (inclusive) and n (exclusive).
-
nextLong
public long nextLong()
Returns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence. All 264 possiblelongvalues should be produced with (approximately) equal probability.- Specified by:
nextLongin interfaceRandomGenerator- Returns:
- the next pseudorandom, uniformly distributed
longvalue from this random number generator's sequence
-
nextLong
public long nextLong(long n)
Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.- Specified by:
nextLongin interfaceRandomGenerator- Parameters:
n- the bound on the random number to be returned. Must be positive.- Returns:
- a pseudorandom, uniformly distributed
intvalue between 0 (inclusive) and n (exclusive).
-
nextBoolean
public boolean nextBoolean()
Returns the next pseudorandom, uniformly distributedbooleanvalue from this random number generator's sequence.- Specified by:
nextBooleanin interfaceRandomGenerator- Returns:
- the next pseudorandom, uniformly distributed
booleanvalue from this random number generator's sequence
-
nextFloat
public float nextFloat()
Returns the next pseudorandom, uniformly distributedfloatvalue between0.0and1.0from this random number generator's sequence.- Specified by:
nextFloatin interfaceRandomGenerator- Returns:
- the next pseudorandom, uniformly distributed
floatvalue between0.0and1.0from this random number generator's sequence
-
nextDouble
public double nextDouble()
Returns the next pseudorandom, uniformly distributeddoublevalue between0.0and1.0from this random number generator's sequence.- Specified by:
nextDoublein interfaceRandomGenerator- Returns:
- the next pseudorandom, uniformly distributed
doublevalue between0.0and1.0from this random number generator's sequence
-
nextGaussian
public double nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributeddoublevalue with mean0.0and standard deviation1.0from this random number generator's sequence.- Specified by:
nextGaussianin interfaceRandomGenerator- Returns:
- the next pseudorandom, Gaussian ("normally") distributed
doublevalue with mean0.0and standard deviation1.0from this random number generator's sequence
-
-