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 RandomGenerator
delegate()
Returns the backing delegate instance that methods are forwarded to.double
nextBeta(double alpha, double beta)
Returns the next pseudo-random beta-distributed value with the given shape and scale parameters.boolean
nextBoolean()
Returns the next pseudorandom, uniformly distributedboolean
value from this random number generator's sequence.void
nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array.void
nextBytes(byte[] bytes, int offset, int length)
Generates random bytes and places them into a user-supplied byte array.int
nextDeviate(IntegerDistribution dist)
Returns a random deviate from the given distribution.double
nextDeviate(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.double
nextDouble()
Returns the next pseudorandom, uniformly distributeddouble
value between0.0
and1.0
from this random number generator's sequence.double
nextExponential(double mean)
Returns the next pseudo-random, exponentially distributed deviate.float
nextFloat()
Returns the next pseudorandom, uniformly distributedfloat
value between0.0
and1.0
from this random number generator's sequence.double
nextGamma(double shape, double scale)
Returns the next pseudo-random gamma-distributed value with the given shape and scale parameters.double
nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributeddouble
value with mean0.0
and standard deviation1.0
from this random number generator's sequence.String
nextHexString(int len)
Generates a random string of hex characters of lengthlen
.int
nextInt()
Returns the next pseudorandom, uniformly distributedint
value from this random number generator's sequence.int
nextInt(int n)
Returns a pseudorandom, uniformly distributedint
value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.int
nextInt(int lower, int upper)
Returns a uniformly distributed random integer between lower and upper (inclusive).double
nextLogNormal(double shape, double scale)
Returns the next log-normally-distributed pseudo-random deviate.long
nextLong()
Returns the next pseudorandom, uniformly distributedlong
value from this random number generator's sequence.long
nextLong(long n)
Returns a pseudorandom, uniformly distributedint
value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.long
nextLong(long lower, long upper)
Returns a uniformly distributed random long integer between lower and upper (inclusive).double
nextNormal(double mean, double standardDeviation)
Returns the next normally-distributed pseudo-random deviate.int[]
nextPermutation(int n, int k)
Generates an integer array of lengthk
whose entries are selected randomly, without repetition, from the integers0, ..., n - 1
(inclusive).int
nextPoisson(double mean)
Returns a poisson-distributed deviate with the given mean.double[]
nextSample(double[] a, int k)
Returns an array ofk
double values selected randomly from the double arraya
.Object[]
nextSample(Collection<?> c, int k)
Returns an array ofk
objects selected randomly from the Collectionc
.int[]
nextSampleWithReplacement(int sampleSize, double[] weights)
Generates a random sample of size sampleSize from {0, 1, ...double
nextUniform(double lower, double upper)
Returns a double value uniformly distributed over [lower, upper]int
nextZipf(int numberOfElements, double exponent)
Returns an integer value following a Zipf distribution with the given parameter.static RandomDataGenerator
of(RandomGenerator randomGenerator)
Factory method to create aRandomData
instance using the suppliedRandomGenerator
.void
setSeed(int seed)
Sets the seed of the underlying random number generator using anint
seed.void
setSeed(int[] seed)
Sets the seed of the underlying random number generator using anint
array seed.void
setSeed(long seed)
Sets the seed of the underlying random number generator using along
seed.-
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 aRandomData
instance 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
size
values 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
size
values 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 MathIllegalArgumentException
Returns 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 + 1
binary 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 MathIllegalArgumentException
Generates an integer array of lengthk
whose entries are selected randomly, without repetition, from the integers0, ..., n - 1
(inclusive).Generated arrays represent permutations of
This method callsn
takenk
at a time.MathArrays.shuffle
in 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 ofk
objects selected randomly from the Collectionc
.Sampling from
c
is without replacement; but ifc
contains identical objects, the sample may include repeats. If all elements ofc
are distinct, the resulting object array represents a Simple Random Sample of sizek
from 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
k
elements fromc
- Throws:
MathIllegalArgumentException
- ifk > c.size()
.MathIllegalArgumentException
- ifk <= 0
.
-
nextSample
public double[] nextSample(double[] a, int k) throws MathIllegalArgumentException
Returns an array ofk
double values selected randomly from the double arraya
.Sampling from
a
is without replacement; but ifa
contains identical objects, the sample may include repeats. If all elements ofa
are distinct, the resulting object array represents a Simple Random Sample of sizek
from the elements ofa
.- Parameters:
a
- the array to be sampledk
- the size of the sample- Returns:
- a random sample of
k
elements 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 anint
seed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeed
in interfaceRandomGenerator
- Parameters:
seed
- the seed value
-
setSeed
public void setSeed(int[] seed)
Sets the seed of the underlying random number generator using anint
array seed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeed
in interfaceRandomGenerator
- Parameters:
seed
- the seed value
-
setSeed
public void setSeed(long seed)
Sets the seed of the underlying random number generator using along
seed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeed
in 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:
nextBytes
in 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:
nextBytes
in 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 distributedint
value from this random number generator's sequence.All 232 possible
int
values should be produced with (approximately) equal probability.- Specified by:
nextInt
in interfaceRandomGenerator
- Returns:
- the next pseudorandom, uniformly distributed
int
value from this random number generator's sequence
-
nextInt
public int nextInt(int n)
Returns a pseudorandom, uniformly distributedint
value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.- Specified by:
nextInt
in interfaceRandomGenerator
- Parameters:
n
- the bound on the random number to be returned. Must be positive.- Returns:
- a pseudorandom, uniformly distributed
int
value between 0 (inclusive) and n (exclusive).
-
nextLong
public long nextLong()
Returns the next pseudorandom, uniformly distributedlong
value from this random number generator's sequence. All 264 possiblelong
values should be produced with (approximately) equal probability.- Specified by:
nextLong
in interfaceRandomGenerator
- Returns:
- the next pseudorandom, uniformly distributed
long
value from this random number generator's sequence
-
nextLong
public long nextLong(long n)
Returns a pseudorandom, uniformly distributedint
value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.- Specified by:
nextLong
in interfaceRandomGenerator
- Parameters:
n
- the bound on the random number to be returned. Must be positive.- Returns:
- a pseudorandom, uniformly distributed
int
value between 0 (inclusive) and n (exclusive).
-
nextBoolean
public boolean nextBoolean()
Returns the next pseudorandom, uniformly distributedboolean
value from this random number generator's sequence.- Specified by:
nextBoolean
in interfaceRandomGenerator
- Returns:
- the next pseudorandom, uniformly distributed
boolean
value from this random number generator's sequence
-
nextFloat
public float nextFloat()
Returns the next pseudorandom, uniformly distributedfloat
value between0.0
and1.0
from this random number generator's sequence.- Specified by:
nextFloat
in interfaceRandomGenerator
- Returns:
- the next pseudorandom, uniformly distributed
float
value between0.0
and1.0
from this random number generator's sequence
-
nextDouble
public double nextDouble()
Returns the next pseudorandom, uniformly distributeddouble
value between0.0
and1.0
from this random number generator's sequence.- Specified by:
nextDouble
in interfaceRandomGenerator
- Returns:
- the next pseudorandom, uniformly distributed
double
value between0.0
and1.0
from this random number generator's sequence
-
nextGaussian
public double nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributeddouble
value with mean0.0
and standard deviation1.0
from this random number generator's sequence.- Specified by:
nextGaussian
in interfaceRandomGenerator
- Returns:
- the next pseudorandom, Gaussian ("normally") distributed
double
value with mean0.0
and standard deviation1.0
from this random number generator's sequence
-
-