public class RandomDataImpl extends java.lang.Object implements RandomData, java.io.Serializable
RandomData
interface using a 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 generator based on Random
. 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 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).Constructor and Description |
---|
RandomDataImpl()
Construct a RandomDataImpl.
|
RandomDataImpl(RandomGenerator rand)
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)
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 a random value from an Exponential distribution with the given
mean.
|
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)
Generate a random value from a Normal (a.k.a.
|
java.lang.String |
nextHexString(int len)
Generates a random string of hex characters of length
len . |
int |
nextHypergeometric(int populationSize,
int numberOfSuccesses,
int sampleSize)
Generates a random value from the
Hypergeometric Distribution . |
int |
nextInt(int lower,
int upper)
Generate a random int value uniformly distributed between
lower and upper , inclusive. |
double |
nextInversionDeviate(ContinuousDistribution distribution)
Generate a random deviate from the given distribution using the
inversion method.
|
int |
nextInversionDeviate(IntegerDistribution distribution)
Generate a random deviate from the given distribution using the
inversion method.
|
long |
nextLong(long lower,
long upper)
Generate a random long value uniformly distributed between
lower and upper , inclusive. |
int |
nextPascal(int r,
double p)
Generates a random value from the
Pascal Distribution . |
int[] |
nextPermutation(int n,
int k)
Generates an integer array of length
k whose entries are
selected randomly, without repetition, from the integers
0 through n-1 (inclusive). |
long |
nextPoisson(double mean)
Generates a random value from the Poisson distribution with
the given mean.
|
java.lang.Object[] |
nextSample(java.util.Collection<?> c,
int k)
Uses a 2-cycle permutation shuffle to generate a random permutation.
|
java.lang.String |
nextSecureHexString(int len)
Generates a random string of hex characters from a secure random
sequence.
|
int |
nextSecureInt(int lower,
int upper)
Generate a random int value uniformly distributed between
lower and upper , inclusive. |
long |
nextSecureLong(long lower,
long upper)
Generate a random long value uniformly distributed between
lower and upper , inclusive. |
double |
nextT(double df)
Generates a random value from the
T Distribution . |
double |
nextUniform(double lower,
double upper)
Generates a uniformly distributed random value from the open interval
(
lower ,upper ) (i.e., endpoints excluded). |
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 the current time in
milliseconds.
|
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(java.lang.String algorithm,
java.lang.String provider)
Sets the PRNG algorithm for the underlying SecureRandom instance using
the Security Provider API.
|
public RandomDataImpl()
public RandomDataImpl(RandomGenerator rand)
RandomGenerator
as
the source of (non-secure) random data.rand
- the source of (non-secure) random datapublic java.lang.String nextHexString(int len)
len
.
The generated string will be random, but not cryptographically
secure. To generate cryptographically secure strings, use
nextSecureHexString
Preconditions:
len > 0
(otherwise an IllegalArgumentException
is thrown.)Algorithm Description: hex strings are generated using a 2-step process.
nextHexString
in interface RandomData
len
- the desired string length.NotStrictlyPositiveException
- if len <= 0
.public int nextInt(int lower, int upper)
lower
and upper
, inclusive.nextInt
in interface RandomData
lower
- the lower bound.upper
- the upper bound.NumberIsTooLargeException
- if lower >= upper
.public long nextLong(long lower, long upper)
lower
and upper
, inclusive.nextLong
in interface RandomData
lower
- the lower bound.upper
- the upper bound.NumberIsTooLargeException
- if lower >= upper
.public java.lang.String nextSecureHexString(int len)
If cryptographic security is not required,
use nextHexString()
.
Preconditions:
len > 0
(otherwise an IllegalArgumentException
is thrown.)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 generated stringNotStrictlyPositiveException
- if len <= 0
.public int nextSecureInt(int lower, int upper)
lower
and upper
, inclusive. This algorithm uses
a secure random number generator.nextSecureInt
in interface RandomData
lower
- the lower bound.upper
- the upper bound.NumberIsTooLargeException
- if lower >= upper
.public long nextSecureLong(long lower, long upper)
lower
and upper
, inclusive. This algorithm uses
a secure random number generator.nextSecureLong
in interface RandomData
lower
- the lower bound.upper
- the upper bound.NumberIsTooLargeException
- if lower >= upper
.public long nextPoisson(double mean)
Definition: Poisson Distribution
Preconditions:
Algorithm Description:
nextPoisson
in interface RandomData
mean
- mean of the Poisson distribution.NotStrictlyPositiveException
- if mean <= 0
.public double nextGaussian(double mu, double sigma)
mu
and the given standard deviation,
sigma
.nextGaussian
in interface RandomData
mu
- the mean of the distributionsigma
- the standard deviation of the distributionNotStrictlyPositiveException
- if sigma <= 0
.public double nextExponential(double mean)
Algorithm Description: Uses the Inversion Method to generate exponentially distributed random values from uniform deviates.
nextExponential
in interface RandomData
mean
- the mean of the distributionNotStrictlyPositiveException
- if mean <= 0
.public double nextUniform(double lower, double upper)
lower
,upper
) (i.e., endpoints excluded).
Definition:
Uniform Distribution lower
and
upper - lower
are the
location and scale parameters, respectively.
Preconditions:
lower < upper
(otherwise an IllegalArgumentException
is thrown.)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 lower bound.upper
- the upper bound.NumberIsTooLargeException
- if lower >= upper
.public double nextBeta(double alpha, double beta) throws MathException
Beta Distribution
.
This implementation uses inversion
to generate random values.alpha
- first distribution shape parameterbeta
- second distribution shape parameterMathException
- if an error occurs generating the random valuepublic int nextBinomial(int numberOfTrials, double probabilityOfSuccess) throws MathException
Binomial Distribution
.
This implementation uses inversion
to generate random values.numberOfTrials
- number of trials of the Binomial distributionprobabilityOfSuccess
- probability of success of the Binomial distributionMathException
- if an error occurs generating the random valuepublic double nextCauchy(double median, double scale) throws MathException
Cauchy Distribution
.
This implementation uses inversion
to generate random values.median
- the median of the Cauchy distributionscale
- the scale parameter of the Cauchy distributionMathException
- if an error occurs generating the random valuepublic double nextChiSquare(double df) throws MathException
ChiSquare Distribution
.
This implementation uses inversion
to generate random values.df
- the degrees of freedom of the ChiSquare distributionMathException
- if an error occurs generating the random valuepublic double nextF(double numeratorDf, double denominatorDf) throws MathException
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 distributionMathException
- if an error occurs generating the random valuepublic double nextGamma(double shape, double scale) throws MathException
Gamma Distribution
.
This implementation uses inversion
to generate random values.shape
- the median of the Gamma distributionscale
- the scale parameter of the Gamma distributionMathException
- if an error occurs generating the random valuepublic int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws MathException
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 distributionMathException
- if an error occurs generating the random valuepublic int nextPascal(int r, double p) throws MathException
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 distributionMathException
- if an error occurs generating the random valuepublic double nextT(double df) throws MathException
T Distribution
.
This implementation uses inversion
to generate random values.df
- the degrees of freedom of the T distributionMathException
- if an error occurs generating the random valuepublic double nextWeibull(double shape, double scale) throws MathException
Weibull Distribution
.
This implementation uses inversion
to generate random values.shape
- the shape parameter of the Weibull distributionscale
- the scale parameter of the Weibull distributionMathException
- if an error occurs generating the random valuepublic int nextZipf(int numberOfElements, double exponent) throws MathException
Zipf Distribution
.
This implementation uses inversion
to generate random values.numberOfElements
- the number of elements of the ZipfDistributionexponent
- the exponent of the ZipfDistributionMathException
- if an error occurs generating the random valuepublic 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()
public void setSecureAlgorithm(java.lang.String algorithm, java.lang.String provider) throws java.security.NoSuchAlgorithmException, java.security.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 providerjava.security.NoSuchAlgorithmException
- if the specified algorithm is not availablejava.security.NoSuchProviderException
- if the specified provider is not installedpublic int[] nextPermutation(int n, int k)
k
whose entries are
selected randomly, without repetition, from the integers
0 through n-1
(inclusive).
Generated arrays represent permutations of n
taken
k
at a time.
Preconditions:
k <= n
n > 0
Uses a 2-cycle permutation shuffle. The shuffling process is described here.
nextPermutation
in interface RandomData
n
- domain of the permutation (must be positive)k
- size of the permutation (must satisfy 0 < k <= n).NumberIsTooLargeException
- if k > n
.NotStrictlyPositiveException
- if k <= 0
.public java.lang.Object[] nextSample(java.util.Collection<?> c, int k)
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,
herenextSample
in interface RandomData
c
- Collection to sample from.k
- sample size.NumberIsTooLargeException
- if k > c.size()
.NotStrictlyPositiveException
- if k <= 0
.public double nextInversionDeviate(ContinuousDistribution distribution) throws MathException
distribution
- Continuous distribution to generate a random value fromMathException
- if an error occurs computing the inverse cumulative distribution functionpublic int nextInversionDeviate(IntegerDistribution distribution) throws MathException
distribution
- Integer distribution to generate a random value fromMathException
- if an error occurs computing the inverse cumulative distribution function"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"