public class RandomDataGenerator extends Object implements Serializable
RandomGenerator
instance to generate non-secure data and a SecureRandom
instance
to provide data for the nextSecureXxx
methods. If no RandomGenerator
is provided in the
constructor, the default is
to use a Well19937c
generator. To plug in a different
implementation, either implement RandomGenerator
directly or
extend AbstractRandomGenerator
.
Supports reseeding the underlying pseudo-random number generator (PRNG). The SecurityProvider
and
Algorithm
used by the SecureRandom
instance can also be reset.
For details on the default PRNGs, see Random
and SecureRandom
.
Usage Notes:
RandomGenerator
and SecureRandom
instances used in
data generation. Therefore, to generate a random sequence of values or strings, you should use just
one RandomDataImpl
instance repeatedly.RandomDataImpl
is created, the underlying random number generators are not
initialized. If you do not explicitly seed the default non-secure generator, it is seeded with the current time in
milliseconds plus the system identity hash code on first use. The same holds for the secure generator. If you provide
a RandomGenerator
to the constructor, however, this generator is not reseeded by the constructor nor is
it reseeded on first use.reSeed
and reSeedSecure
methods delegate to the corresponding methods on the underlying
RandomGenerator
and SecureRandom
instances. Therefore, reSeed(long)
fully
resets the initial state of the non-secure random number generator (so that reseeding with a specific value always
results in the same subsequent random sequence); whereas reSeedSecure(long) does not reinitialize
the secure random number generator (so secure sequences started with calls to reseedSecure(long) won't be identical).
RandomGenerator
or SecureRandom
instances are not protected by synchronization and are not guaranteed to be thread-safe. Therefore, if an instance of
this class is concurrently utilized by multiple threads, it is the responsibility of client code to synchronize
access to seeding and data generation methods.Constructor and Description |
---|
RandomDataGenerator()
Construct a RandomDataGenerator, using a default random generator as the source
of randomness.
|
RandomDataGenerator(RandomGenerator randIn)
Construct a RandomDataGenerator using the supplied
RandomGenerator as
the source of (non-secure) random data. |
Modifier and Type | Method and Description |
---|---|
RandomGenerator |
getRan()
Returns the RandomGenerator used to generate non-secure random data.
|
double |
nextBeta(double alpha,
double beta)
Generates a random value from the
Beta Distribution . |
int |
nextBinomial(int numberOfTrials,
double probabilityOfSuccess)
Generates a random value from the
Binomial Distribution . |
double |
nextCauchy(double median,
double scale)
Generates a random value from the
Cauchy Distribution . |
double |
nextChiSquare(double df)
Generates a random value from the
ChiSquare Distribution . |
double |
nextExponential(double mean)
Returns random long following Exponential distribution.
|
double |
nextF(double numeratorDf,
double denominatorDf)
Generates a random value from the
F Distribution . |
double |
nextGamma(double shape,
double scale)
Generates a random value from the
Gamma
Distribution . |
double |
nextGaussian(double mu,
double sigma)
Returns random double following Gaussian distribution.
|
String |
nextHexString(int len)
Algorithm Description: hex strings are generated using a 2-step process.
|
int |
nextHypergeometric(int populationSize,
int numberOfSuccesses,
int sampleSize)
Generates a random value from the
Hypergeometric Distribution . |
int |
nextInt(int lower,
int upper)
Returns random int in [lower, upper] (uniform distribution).
|
long |
nextLong(long lower,
long upper)
Returns random long in [lower, upper] (uniform distribution).
|
int |
nextPascal(int r,
double p)
Generates a random value from the
Pascal Distribution . |
int[] |
nextPermutation(int n,
int k)
Returns random permutation.
|
long |
nextPoisson(double mean)
Returns random long following Poisson distribution.
|
Object[] |
nextSample(Collection<?> c,
int k)
Returns random sample in collection.
|
String |
nextSecureHexString(int len)
Algorithm Description: hex strings are generated in 40-byte segments using a 3-step process.
|
int |
nextSecureInt(int lower,
int upper)
Returns random int in [lower, upper] (uniform distribution).
|
long |
nextSecureLong(long lower,
long upper)
Returns random int in [lower, upper] (uniform distribution).
|
double |
nextT(double df)
Generates a random value from the
T Distribution . |
double |
nextUniform(double lower,
double upper)
Returns random double following Uniform distribution.
|
double |
nextUniform(double lower,
double upper,
boolean lowerInclusive)
Returns random double following Uniform distribution.
|
double |
nextWeibull(double shape,
double scale)
Generates a random value from the
Weibull Distribution . |
int |
nextZipf(int numberOfElements,
double exponent)
Generates a random value from the
Zipf Distribution . |
void |
reSeed()
Reseeds the random number generator with
System.currentTimeMillis() + System.identityHashCode(this)) . |
void |
reSeed(long seed)
Reseeds the random number generator with the supplied seed.
|
void |
reSeedSecure()
Reseeds the secure random number generator with the current time in
milliseconds.
|
void |
reSeedSecure(long seed)
Reseeds the secure random number generator with the supplied seed.
|
void |
setSecureAlgorithm(String algorithm,
String provider)
Sets the PRNG algorithm for the underlying SecureRandom instance using
the Security Provider API.
|
public RandomDataGenerator()
The default generator is a Well19937c
seeded with
System.currentTimeMillis() + System.identityHashCode(this))
. The generator is initialized and seeded on
first use.
public RandomDataGenerator(RandomGenerator randIn)
RandomGenerator
as
the source of (non-secure) random data.randIn
- the source of (non-secure) random data
(may be null, resulting in the default generator)public String nextHexString(int len)
Algorithm Description: hex strings are generated using a 2-step process.
len / 2 + 1
binary bytes are generated using the underlying Randomlen
- the desired string length.NotStrictlyPositiveException
- if len <= 0
.public int nextInt(int lower, int upper)
lower
- lower boundupper
- upper boundNumberIsTooLargeException
- thrown if lower >= upperpublic long nextLong(long lower, long upper)
lower
- lower boundupper
- upper boundNumberIsTooLargeException
- thrown if lower >= upperpublic String nextSecureHexString(int len)
Algorithm Description: hex strings are generated in 40-byte segments using a 3-step process.
SecureRandom
.len
- String lengthNotStrictlyPositiveException
- if len <= 0
public int nextSecureInt(int lower, int upper)
lower
- lower boundupper
- upper boundNumberIsTooLargeException
- thrown if lower >= upperSecureRandom
public long nextSecureLong(long lower, long upper)
lower
- lower boundupper
- upper boundNumberIsTooLargeException
- thrown if lower >= upperSecureRandom
public long nextPoisson(double mean)
mean
- meanAlgorithm Description:
NotStrictlyPositiveException
- if len <= 0
public double nextGaussian(double mu, double sigma)
mu
- centersigma
- standard deviationNotStrictlyPositiveException
- thrown if sigma <= 0public double nextExponential(double mean)
mean
- meanAlgorithm Description: Uses the Algorithm SA (Ahrens) from p. 876 in: [1]: Ahrens, J. H. and Dieter, U. (1972). Computer methods for sampling from the exponential and normal distributions. Communications of the ACM, 15, 873-882.
NotStrictlyPositiveException
- thrown if mean <= 0public double nextGamma(double shape, double scale)
Generates a random value from the Gamma
Distribution
.
This implementation uses the following algorithms:
For 0 < shape < 1:
Ahrens, J. H. and Dieter, U., Computer methods for sampling from gamma, beta, Poisson and binomial
distributions. Computing, 12, 223-246, 1974.
For shape >= 1:
Marsaglia and Tsang, A Simple Method for Generating Gamma Variables. ACM Transactions on Mathematical
Software, Volume 26 Issue 3, September, 2000.
shape
- the median of the Gamma distributionscale
- the scale parameter of the Gamma distributionNotStrictlyPositiveException
- if shape <= 0
or scale <= 0
.public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize)
Hypergeometric Distribution
.populationSize
- the population size of the Hypergeometric distributionnumberOfSuccesses
- number of successes in the population of the Hypergeometric distributionsampleSize
- the sample size of the Hypergeometric distributionNumberIsTooLargeException
- if numberOfSuccesses > populationSize
,
or sampleSize > populationSize
.NotStrictlyPositiveException
- if populationSize <= 0
.NotPositiveException
- if numberOfSuccesses < 0
.public int nextPascal(int r, double p)
Pascal Distribution
.r
- the number of successes of the Pascal distributionp
- the probability of success of the Pascal distributionNotStrictlyPositiveException
- if the number of successes is not positiveOutOfRangeException
- if the probability of success is not in the
range [0, 1]
.public double nextT(double df)
T Distribution
.df
- the degrees of freedom of the T distributionNotStrictlyPositiveException
- if df <= 0
public double nextWeibull(double shape, double scale)
Weibull Distribution
.shape
- the shape parameter of the Weibull distributionscale
- the scale parameter of the Weibull distributionNotStrictlyPositiveException
- if shape <= 0
or scale <= 0
.public int nextZipf(int numberOfElements, double exponent)
Zipf Distribution
.numberOfElements
- the number of elements of the ZipfDistributionexponent
- the exponent of the ZipfDistributionNotStrictlyPositiveException
- if numberOfElements <= 0
or exponent <= 0
.public double nextBeta(double alpha, double beta)
Beta Distribution
.alpha
- first distribution shape parameterbeta
- second distribution shape parameterpublic int nextBinomial(int numberOfTrials, double probabilityOfSuccess)
Binomial Distribution
.numberOfTrials
- number of trials of the Binomial distributionprobabilityOfSuccess
- probability of success of the Binomial distributionpublic double nextCauchy(double median, double scale)
Cauchy Distribution
.median
- the median of the Cauchy distributionscale
- the scale parameter of the Cauchy distributionpublic double nextChiSquare(double df)
ChiSquare Distribution
.df
- the degrees of freedom of the ChiSquare distributionpublic double nextF(double numeratorDf, double denominatorDf)
F Distribution
.numeratorDf
- the numerator degrees of freedom of the F distributiondenominatorDf
- the denominator degrees of freedom of the F distributionNotStrictlyPositiveException
- if numeratorDf <= 0
or denominatorDf <= 0
.public double nextUniform(double lower, double upper)
lower
- lower distribution boundupper
- upper distribution boundAlgorithm Description: scales the output of Random.nextDouble(), but rejects 0 values (i.e., will generate another random double if Random.nextDouble() returns 0). This is necessary to provide a symmetric output interval (both endpoints excluded).
NumberIsTooLargeException
- if lower >= upper
NotFiniteNumberException
- if one of the bounds is infiniteNotANumberException
- if one of the bounds is NaNpublic double nextUniform(double lower, double upper, boolean lowerInclusive)
lower
- lower distribution boundupper
- upper distribution boundlowerInclusive
- true if lower bound is includedAlgorithm Description: if the lower bound is excluded, scales the output of Random.nextDouble(), but rejects 0 values (i.e., will generate another random double if Random.nextDouble() returns 0). This is necessary to provide a symmetric output interval (both endpoints excluded).
NumberIsTooLargeException
- if lower >= upper
NotFiniteNumberException
- if one of the bounds is infiniteNotANumberException
- if one of the bounds is NaNpublic int[] nextPermutation(int n, int k)
n
- 1st permutation parameterk
- 2nd permutation parameterUses a 2-cycle permutation shuffle. The shuffling process is described here.
NumberIsTooLargeException
- if k > n
.NotStrictlyPositiveException
- if k <= 0
.public Object[] nextSample(Collection<?> c, int k)
c
- collectionk
- number of samples
Algorithm Description: Uses a 2-cycle permutation shuffle to generate a random
permutation of c.size()
and then returns the elements whose indexes correspond to the
elements of the generated permutation. This technique is described, and proven to generate random samples
here
NumberIsTooLargeException
- thrown if k > size of cNotStrictlyPositiveException
- thrown if k <= 0public void reSeed(long seed)
Will create and initialize if null.
seed
- the seed value to usepublic void reSeedSecure()
Will create and initialize if null.
public void reSeedSecure(long seed)
Will create and initialize if null.
seed
- the seed value to usepublic void reSeed()
System.currentTimeMillis() + System.identityHashCode(this))
.public void setSecureAlgorithm(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
USAGE NOTE: This method carries significant overhead and may take several seconds to execute.
algorithm
- the name of the PRNG algorithmprovider
- the name of the providerNoSuchAlgorithmException
- if the specified algorithm is not availableNoSuchProviderException
- if the specified provider is not installedpublic RandomGenerator getRan()
Creates and initializes a default generator if null. Uses a Well19937c
generator with
System.currentTimeMillis() + System.identityHashCode(this))
as the default seed.
Copyright © 2019 CNES. All rights reserved.