RandomDataGenerator
instead@Deprecated public class RandomDataImpl extends Object implements RandomData, 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 RandomDataGenerator
instance repeatedly.RandomDataGenerator
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 |
---|
RandomDataImpl()
Deprecated.
Construct a RandomDataImpl, using a default random generator as the source
of randomness.
|
RandomDataImpl(RandomGenerator rand)
Deprecated.
Construct a RandomDataImpl using the supplied
RandomGenerator as
the source of (non-secure) random data. |
Modifier and Type | Method and Description |
---|---|
double |
nextBeta(double alpha,
double beta)
Deprecated.
Generates a random value from the
Beta Distribution . |
int |
nextBinomial(int numberOfTrials,
double probabilityOfSuccess)
Deprecated.
Generates a random value from the
Binomial Distribution . |
double |
nextCauchy(double median,
double scale)
Deprecated.
Generates a random value from the
Cauchy Distribution . |
double |
nextChiSquare(double df)
Deprecated.
Generates a random value from the
ChiSquare Distribution . |
double |
nextExponential(double mean)
Deprecated.
Generates a random value from the exponential distribution
with specified mean.
|
double |
nextF(double numeratorDf,
double denominatorDf)
Deprecated.
Generates a random value from the
F Distribution . |
double |
nextGamma(double shape,
double scale)
Deprecated.
Generates a random value from the
Gamma Distribution . |
double |
nextGaussian(double mu,
double sigma)
Deprecated.
Generates a random value from the Normal (or Gaussian) distribution with
specified mean and standard deviation.
|
String |
nextHexString(int len)
Deprecated.
Generates a random string of hex characters of length
len . |
int |
nextHypergeometric(int populationSize,
int numberOfSuccesses,
int sampleSize)
Deprecated.
Generates a random value from the
Hypergeometric Distribution . |
int |
nextInt(int lower,
int upper)
Deprecated.
Generates a uniformly distributed random integer between
lower
and upper (endpoints included). |
int |
nextInversionDeviate(IntegerDistribution distribution)
Deprecated.
use the distribution's sample() method
|
double |
nextInversionDeviate(RealDistribution distribution)
Deprecated.
use the distribution's sample() method
|
long |
nextLong(long lower,
long upper)
Deprecated.
Generates a uniformly distributed random long integer between
lower and upper (endpoints included). |
int |
nextPascal(int r,
double p)
Deprecated.
Generates a random value from the
Pascal Distribution . |
int[] |
nextPermutation(int n,
int k)
Deprecated.
Generates an integer array of length
k whose entries are selected
randomly, without repetition, from the integers 0, ..., n - 1
(inclusive). |
long |
nextPoisson(double mean)
Deprecated.
Generates a random value from the Poisson distribution with the given
mean.
|
Object[] |
nextSample(Collection<?> c,
int k)
Deprecated.
Returns an array of
k objects selected randomly from the
Collection c . |
String |
nextSecureHexString(int len)
Deprecated.
Generates a random string of hex characters from a secure random
sequence.
|
int |
nextSecureInt(int lower,
int upper)
Deprecated.
Generates a uniformly distributed random integer between
lower
and upper (endpoints included) from a secure random sequence. |
long |
nextSecureLong(long lower,
long upper)
Deprecated.
Generates a uniformly distributed random long integer between
lower and upper (endpoints included) from a secure random
sequence. |
double |
nextT(double df)
Deprecated.
Generates a random value from the
T Distribution . |
double |
nextUniform(double lower,
double upper)
Deprecated.
Generates a uniformly distributed random value from the open interval
(lower, upper) (i.e., endpoints excluded). |
double |
nextUniform(double lower,
double upper,
boolean lowerInclusive)
Deprecated.
Generates a uniformly distributed random value from the interval
(lower, upper) or the interval [lower, upper) . |
double |
nextWeibull(double shape,
double scale)
Deprecated.
Generates a random value from the
Weibull Distribution . |
int |
nextZipf(int numberOfElements,
double exponent)
Deprecated.
Generates a random value from the
Zipf Distribution . |
void |
reSeed()
Deprecated.
Reseeds the random number generator with
System.currentTimeMillis() + System.identityHashCode(this)) . |
void |
reSeed(long seed)
Deprecated.
Reseeds the random number generator with the supplied seed.
|
void |
reSeedSecure()
Deprecated.
Reseeds the secure random number generator with the current time in
milliseconds.
|
void |
reSeedSecure(long seed)
Deprecated.
Reseeds the secure random number generator with the supplied seed.
|
void |
setSecureAlgorithm(String algorithm,
String provider)
Deprecated.
Sets the PRNG algorithm for the underlying SecureRandom instance using
the Security Provider API.
|
public RandomDataImpl()
The default generator is a Well19937c
seeded
with System.currentTimeMillis() + System.identityHashCode(this))
.
The generator is initialized and seeded on first use.
public RandomDataImpl(RandomGenerator rand)
RandomGenerator
as
the source of (non-secure) random data.rand
- the source of (non-secure) random data
(may be null, resulting in the default generator)public String nextHexString(int len) throws NotStrictlyPositiveException
len
.
The generated string will be random, but not cryptographically
secure. To generate cryptographically secure strings, use
RandomData.nextSecureHexString(int)
.
Algorithm Description: hex strings are generated using a 2-step process.
len / 2 + 1
binary bytes are generated using the underlying
RandomnextHexString
in interface RandomData
len
- the desired string length.NotStrictlyPositiveException
- if len <= 0
.public int nextInt(int lower, int upper) throws NumberIsTooLargeException
lower
and upper
(endpoints included).
The generated integer will be random, but not cryptographically secure.
To generate cryptographically secure integer sequences, use
RandomData.nextSecureInt(int, int)
.
nextInt
in interface RandomData
lower
- lower bound for generated integerupper
- upper bound for generated integerlower
and less than or equal to upper
NumberIsTooLargeException
- if lower >= upper
public long nextLong(long lower, long upper) throws NumberIsTooLargeException
lower
and upper
(endpoints included).
The generated long integer values will be random, but not
cryptographically secure. To generate cryptographically secure sequences
of longs, use RandomData.nextSecureLong(long, long)
.
nextLong
in interface RandomData
lower
- lower bound for generated long integerupper
- upper bound for generated long integerlower
and
less than or equal to upper
NumberIsTooLargeException
- if lower >= upper
public String nextSecureHexString(int len) throws NotStrictlyPositiveException
If cryptographic security is not required, use
RandomData.nextHexString(int)
.
Algorithm Description: hex strings are generated in 40-byte segments using a 3-step process.
SecureRandom
.nextSecureHexString
in interface RandomData
len
- the length of the string to be generatedlen
NotStrictlyPositiveException
- if len <= 0
public int nextSecureInt(int lower, int upper) throws NumberIsTooLargeException
lower
and upper
(endpoints included) from a secure random sequence.
Sequences of integers generated using this method will be
cryptographically secure. If cryptographic security is not required,
RandomData.nextInt(int, int)
should be used instead of this method.
Definition: Secure Random Sequence
nextSecureInt
in interface RandomData
lower
- lower bound for generated integerupper
- upper bound for generated integerlower
and less
than or equal to upper
.NumberIsTooLargeException
- if lower >= upper
.public long nextSecureLong(long lower, long upper) throws NumberIsTooLargeException
lower
and upper
(endpoints included) from a secure random
sequence.
Sequences of long values generated using this method will be
cryptographically secure. If cryptographic security is not required,
RandomData.nextLong(long, long)
should be used instead of this method.
Definition: Secure Random Sequence
nextSecureLong
in interface RandomData
lower
- lower bound for generated integerupper
- upper bound for generated integerlower
and
less than or equal to upper
.NumberIsTooLargeException
- if lower >= upper
.public long nextPoisson(double mean) throws NotStrictlyPositiveException
Definition: Poisson Distribution
Algorithm Description:
nextPoisson
in interface RandomData
mean
- the mean of the Poisson distributionNotStrictlyPositiveException
- if mean <= 0
.public double nextGaussian(double mu, double sigma) throws NotStrictlyPositiveException
Definition: Normal Distribution
nextGaussian
in interface RandomData
mu
- the mean of the distributionsigma
- the standard deviation of the distributionNotStrictlyPositiveException
- if sigma <= 0
.public double nextExponential(double mean) throws NotStrictlyPositiveException
Definition: Exponential Distribution
Algorithm 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.
nextExponential
in interface RandomData
mean
- the mean of the distributionNotStrictlyPositiveException
- if mean <= 0
.public double nextUniform(double lower, double upper) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException
(lower, upper)
(i.e., endpoints excluded).
Definition:
Uniform Distribution lower
and upper - lower
are the
location and scale parameters, respectively.
Algorithm 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).
nextUniform
in interface RandomData
lower
- the exclusive lower bound of the supportupper
- the exclusive upper bound of the supportNumberIsTooLargeException
- 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) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException
(lower, upper)
or the interval [lower, upper)
. The lower
bound is thus optionally included, while the upper bound is always
excluded.
Definition:
Uniform Distribution lower
and upper - lower
are the
location and scale parameters, respectively.
Algorithm 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).
nextUniform
in interface RandomData
lower
- the lower bound of the supportupper
- the exclusive upper bound of the supportlowerInclusive
- true
if the lower bound is inclusive(lower, upper)
interval, if lowerInclusive
is false
, or in the
[lower, upper)
interval, if lowerInclusive
is
true
NumberIsTooLargeException
- if lower >= upper
NotFiniteNumberException
- if one of the bounds is infiniteNotANumberException
- if one of the bounds is NaNpublic double nextBeta(double alpha, double beta)
Beta Distribution
.
This implementation uses inversion
to generate random values.alpha
- first distribution shape parameterbeta
- second distribution shape parameterpublic int nextBinomial(int numberOfTrials, double probabilityOfSuccess)
Binomial Distribution
.
This implementation uses inversion
to generate random values.numberOfTrials
- number of trials of the Binomial distributionprobabilityOfSuccess
- probability of success of the Binomial distributionpublic double nextCauchy(double median, double scale)
Cauchy Distribution
.
This implementation uses inversion
to generate random values.median
- the median of the Cauchy distributionscale
- the scale parameter of the Cauchy distributionpublic double nextChiSquare(double df)
ChiSquare Distribution
.
This implementation uses inversion
to generate random values.df
- the degrees of freedom of the ChiSquare distributionpublic double nextF(double numeratorDf, double denominatorDf) throws NotStrictlyPositiveException
F Distribution
.
This implementation uses inversion
to generate random values.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 nextGamma(double shape, double scale) throws NotStrictlyPositiveException
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) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException
Hypergeometric Distribution
.
This implementation uses inversion
to generate random values.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) throws NotStrictlyPositiveException, OutOfRangeException
Pascal Distribution
.
This implementation uses inversion
to generate random values.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) throws NotStrictlyPositiveException
T Distribution
.
This implementation uses inversion
to generate random values.df
- the degrees of freedom of the T distributionNotStrictlyPositiveException
- if df <= 0
public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException
Weibull Distribution
.
This implementation uses inversion
to generate random values.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) throws NotStrictlyPositiveException
Zipf Distribution
.
This implementation uses inversion
to generate random values.numberOfElements
- the number of elements of the ZipfDistributionexponent
- the exponent of the ZipfDistributionNotStrictlyPositiveException
- if numberOfElements <= 0
or exponent <= 0
.public 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 int[] nextPermutation(int n, int k) throws NotStrictlyPositiveException, NumberIsTooLargeException
k
whose entries are selected
randomly, without repetition, from the integers 0, ..., n - 1
(inclusive).
Generated arrays represent permutations of n
taken k
at a
time.
Uses a 2-cycle permutation shuffle. The shuffling process is described here.
nextPermutation
in interface RandomData
n
- the domain of the permutationk
- the size of the permutationk
-permutation of n
, as an array of
integersNotStrictlyPositiveException
- if k <= 0
.NumberIsTooLargeException
- if k > n
.public Object[] nextSample(Collection<?> c, int k) throws NotStrictlyPositiveException, NumberIsTooLargeException
k
objects selected randomly from the
Collection c
.
Sampling from c
is without replacement; but if c
contains
identical objects, the sample may include repeats. If all elements of
c
are distinct, the resulting object array represents a
Simple Random Sample of size k
from the elements of
c
.
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
nextSample
in interface RandomData
c
- the collection to be sampledk
- the size of the samplek
elements from c
NotStrictlyPositiveException
- if k <= 0
.NumberIsTooLargeException
- if k > c.size()
.@Deprecated public double nextInversionDeviate(RealDistribution distribution) throws MathIllegalArgumentException
distribution
- Continuous distribution to generate a random value fromMathIllegalArgumentException
- if the underlynig distribution throws one@Deprecated public int nextInversionDeviate(IntegerDistribution distribution) throws MathIllegalArgumentException
distribution
- Integer distribution to generate a random value fromMathIllegalArgumentException
- if the underlynig distribution throws oneCopyright © 2003–2016 The Apache Software Foundation. All rights reserved.