Class RandomDataImpl
- All Implemented Interfaces:
Serializable,RandomData
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:
-
Instance variables are used to maintain
RandomGeneratorandSecureRandominstances used in data generation. Therefore, to generate a random sequence of values or strings, you should use just oneRandomDataGeneratorinstance repeatedly. - The "secure" methods are *much* slower. These should be used only when a cryptographically secure random sequence is required. A secure random sequence is a sequence of pseudo-random values which, in addition to being well-dispersed (so no subsequence of values is an any more likely than other subsequence of the the same length), also has the additional property that knowledge of values generated up to any point in the sequence does not make it any easier to predict subsequent values.
-
When a new
RandomDataGeneratoris 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 aRandomGeneratorto the constructor, however, this generator is not reseeded by the constructor nor is it reseeded on first use. -
The
reSeedandreSeedSecuremethods delegate to the corresponding methods on the underlyingRandomGeneratorandSecureRandominstances. 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). -
This implementation is not synchronized. The underlying
RandomGeneratororSecureRandominstances 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.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Construct a RandomDataImpl, using a default random generator as the source of randomness.Deprecated.Construct a RandomDataImpl using the suppliedRandomGeneratoras the source of (non-secure) random data. -
Method Summary
Modifier and TypeMethodDescriptiondoublenextBeta(double alpha, double beta) Deprecated.Generates a random value from theBeta Distribution.intnextBinomial(int numberOfTrials, double probabilityOfSuccess) Deprecated.Generates a random value from theBinomial Distribution.doublenextCauchy(double median, double scale) Deprecated.Generates a random value from theCauchy Distribution.doublenextChiSquare(double df) Deprecated.Generates a random value from theChiSquare Distribution.doublenextExponential(double mean) Deprecated.Generates a random value from the exponential distribution with specified mean.doublenextF(double numeratorDf, double denominatorDf) Deprecated.Generates a random value from theF Distribution.doublenextGamma(double shape, double scale) Deprecated.Generates a random value from theGamma Distribution.doublenextGaussian(double mu, double sigma) Deprecated.Generates a random value from the Normal (or Gaussian) distribution with specified mean and standard deviation.nextHexString(int len) Deprecated.Generates a random string of hex characters of lengthlen.intnextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) Deprecated.Generates a random value from theHypergeometric Distribution.intnextInt(int lower, int upper) Deprecated.Generates a uniformly distributed random integer betweenlowerandupper(endpoints included).intnextInversionDeviate(IntegerDistribution distribution) Deprecated.use the distribution's sample() methoddoublenextInversionDeviate(RealDistribution distribution) Deprecated.use the distribution's sample() methodlongnextLong(long lower, long upper) Deprecated.Generates a uniformly distributed random long integer betweenlowerandupper(endpoints included).intnextPascal(int r, double p) Deprecated.Generates a random value from thePascal Distribution.int[]nextPermutation(int n, int k) Deprecated.Generates an integer array of lengthkwhose entries are selected randomly, without repetition, from the integers0, ..., n - 1(inclusive).longnextPoisson(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 ofkobjects selected randomly from the Collectionc.nextSecureHexString(int len) Deprecated.Generates a random string of hex characters from a secure random sequence.intnextSecureInt(int lower, int upper) Deprecated.Generates a uniformly distributed random integer betweenlowerandupper(endpoints included) from a secure random sequence.longnextSecureLong(long lower, long upper) Deprecated.Generates a uniformly distributed random long integer betweenlowerandupper(endpoints included) from a secure random sequence.doublenextT(double df) Deprecated.Generates a random value from theT Distribution.doublenextUniform(double lower, double upper) Deprecated.Generates a uniformly distributed random value from the open interval(lower, upper)(i.e., endpoints excluded).doublenextUniform(double lower, double upper, boolean lowerInclusive) Deprecated.Generates a uniformly distributed random value from the interval(lower, upper)or the interval[lower, upper).doublenextWeibull(double shape, double scale) Deprecated.Generates a random value from theWeibull Distribution.intnextZipf(int numberOfElements, double exponent) Deprecated.Generates a random value from theZipf Distribution.voidreSeed()Deprecated.Reseeds the random number generator withSystem.currentTimeMillis() + System.identityHashCode(this)).voidreSeed(long seed) Deprecated.Reseeds the random number generator with the supplied seed.voidDeprecated.Reseeds the secure random number generator with the current time in milliseconds.voidreSeedSecure(long seed) Deprecated.Reseeds the secure random number generator with the supplied seed.voidsetSecureAlgorithm(String algorithm, String provider) Deprecated.Sets the PRNG algorithm for the underlying SecureRandom instance using the Security Provider API.
-
Constructor Details
-
RandomDataImpl
public RandomDataImpl()Deprecated.Construct a RandomDataImpl, using a default random generator as the source of randomness.The default generator is a
Well19937cseeded withSystem.currentTimeMillis() + System.identityHashCode(this)). The generator is initialized and seeded on first use. -
RandomDataImpl
Deprecated.Construct a RandomDataImpl using the suppliedRandomGeneratoras the source of (non-secure) random data.- Parameters:
rand- the source of (non-secure) random data (may be null, resulting in the default generator)- Since:
- 1.1
-
-
Method Details
-
nextHexString
Deprecated.Generates a random string of hex characters of lengthlen.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 + 1binary bytes are generated using the underlying Random- Each binary byte is translated into 2 hex digits
- Specified by:
nextHexStringin interfaceRandomData- Parameters:
len- the desired string length.- Returns:
- the random string.
- Throws:
NotStrictlyPositiveException- iflen <= 0.
-
nextInt
Deprecated.Generates a uniformly distributed random integer betweenlowerandupper(endpoints included).The generated integer will be random, but not cryptographically secure. To generate cryptographically secure integer sequences, use
RandomData.nextSecureInt(int, int).- Specified by:
nextIntin interfaceRandomData- Parameters:
lower- lower bound for generated integerupper- upper bound for generated integer- Returns:
- a random integer greater than or equal to
lowerand less than or equal toupper - Throws:
NumberIsTooLargeException- iflower >= upper
-
nextLong
Deprecated.Generates a uniformly distributed random long integer betweenlowerandupper(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).- Specified by:
nextLongin interfaceRandomData- Parameters:
lower- lower bound for generated long integerupper- upper bound for generated long integer- Returns:
- a random long integer greater than or equal to
lowerand less than or equal toupper - Throws:
NumberIsTooLargeException- iflower >= upper
-
nextSecureHexString
Deprecated.Generates a random string of hex characters from a secure random sequence.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.
-
20 random bytes are generated using the underlying
SecureRandom. - SHA-1 hash is applied to yield a 20-byte binary digest.
- Each byte of the binary digest is converted to 2 hex digits.
- Specified by:
nextSecureHexStringin interfaceRandomData- Parameters:
len- the length of the string to be generated- Returns:
- a random string of hex characters of length
len - Throws:
NotStrictlyPositiveException- iflen <= 0
-
20 random bytes are generated using the underlying
-
nextSecureInt
Deprecated.Generates a uniformly distributed random integer betweenlowerandupper(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
- Specified by:
nextSecureIntin interfaceRandomData- Parameters:
lower- lower bound for generated integerupper- upper bound for generated integer- Returns:
- a random integer greater than or equal to
lowerand less than or equal toupper. - Throws:
NumberIsTooLargeException- iflower >= upper.
-
nextSecureLong
Deprecated.Generates a uniformly distributed random long integer betweenlowerandupper(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
- Specified by:
nextSecureLongin interfaceRandomData- Parameters:
lower- lower bound for generated integerupper- upper bound for generated integer- Returns:
- a random long integer greater than or equal to
lowerand less than or equal toupper. - Throws:
NumberIsTooLargeException- iflower >= upper.
-
nextPoisson
Deprecated.Generates a random value from the Poisson distribution with the given mean.Definition: Poisson Distribution
Algorithm Description:
- For small means, uses simulation of a Poisson process using Uniform deviates, as described here. The Poisson process (and hence value returned) is bounded by 1000 * mean.
- For large means, uses the rejection algorithm described in
Devroye, Luc. (1981).The Computer Generation of Poisson Random Variables Computing vol. 26 pp. 197-207.
- Specified by:
nextPoissonin interfaceRandomData- Parameters:
mean- the mean of the Poisson distribution- Returns:
- a random value following the specified Poisson distribution
- Throws:
NotStrictlyPositiveException- ifmean <= 0.
-
nextGaussian
Deprecated.Generates a random value from the Normal (or Gaussian) distribution with specified mean and standard deviation.Definition: Normal Distribution
- Specified by:
nextGaussianin interfaceRandomData- Parameters:
mu- the mean of the distributionsigma- the standard deviation of the distribution- Returns:
- a random value following the specified Gaussian distribution
- Throws:
NotStrictlyPositiveException- ifsigma <= 0.
-
nextExponential
Deprecated.Generates a random value from the exponential distribution with specified mean.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.
- Specified by:
nextExponentialin interfaceRandomData- Parameters:
mean- the mean of the distribution- Returns:
- a random value following the specified exponential distribution
- Throws:
NotStrictlyPositiveException- ifmean <= 0.
-
nextUniform
public double nextUniform(double lower, double upper) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException Deprecated.Generates a uniformly distributed random value from the open interval(lower, upper)(i.e., endpoints excluded).Definition: Uniform Distribution
lowerandupper - lowerare 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).
- Specified by:
nextUniformin interfaceRandomData- Parameters:
lower- the exclusive lower bound of the supportupper- the exclusive upper bound of the support- Returns:
- a uniformly distributed random value between lower and upper (exclusive)
- Throws:
NumberIsTooLargeException- iflower >= upperNotFiniteNumberException- if one of the bounds is infiniteNotANumberException- if one of the bounds is NaN
-
nextUniform
public double nextUniform(double lower, double upper, boolean lowerInclusive) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException Deprecated.Generates a uniformly distributed random value from the interval(lower, upper)or the interval[lower, upper). The lower bound is thus optionally included, while the upper bound is always excluded.Definition: Uniform Distribution
lowerandupper - lowerare 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).
- Specified by:
nextUniformin interfaceRandomData- Parameters:
lower- the lower bound of the supportupper- the exclusive upper bound of the supportlowerInclusive-trueif the lower bound is inclusive- Returns:
- uniformly distributed random value in the
(lower, upper)interval, iflowerInclusiveisfalse, or in the[lower, upper)interval, iflowerInclusiveistrue - Throws:
NumberIsTooLargeException- iflower >= upperNotFiniteNumberException- if one of the bounds is infiniteNotANumberException- if one of the bounds is NaN- Since:
- 3.0
-
nextBeta
public double nextBeta(double alpha, double beta) Deprecated.Generates a random value from theBeta Distribution. This implementation usesinversionto generate random values.- Parameters:
alpha- first distribution shape parameterbeta- second distribution shape parameter- Returns:
- random value sampled from the beta(alpha, beta) distribution
- Since:
- 2.2
-
nextBinomial
public int nextBinomial(int numberOfTrials, double probabilityOfSuccess) Deprecated.Generates a random value from theBinomial Distribution. This implementation usesinversionto generate random values.- Parameters:
numberOfTrials- number of trials of the Binomial distributionprobabilityOfSuccess- probability of success of the Binomial distribution- Returns:
- random value sampled from the Binomial(numberOfTrials, probabilityOfSuccess) distribution
- Since:
- 2.2
-
nextCauchy
public double nextCauchy(double median, double scale) Deprecated.Generates a random value from theCauchy Distribution. This implementation usesinversionto generate random values.- Parameters:
median- the median of the Cauchy distributionscale- the scale parameter of the Cauchy distribution- Returns:
- random value sampled from the Cauchy(median, scale) distribution
- Since:
- 2.2
-
nextChiSquare
public double nextChiSquare(double df) Deprecated.Generates a random value from theChiSquare Distribution. This implementation usesinversionto generate random values.- Parameters:
df- the degrees of freedom of the ChiSquare distribution- Returns:
- random value sampled from the ChiSquare(df) distribution
- Since:
- 2.2
-
nextF
Deprecated.Generates a random value from theF Distribution. This implementation usesinversionto generate random values.- Parameters:
numeratorDf- the numerator degrees of freedom of the F distributiondenominatorDf- the denominator degrees of freedom of the F distribution- Returns:
- random value sampled from the F(numeratorDf, denominatorDf) distribution
- Throws:
NotStrictlyPositiveException- ifnumeratorDf <= 0ordenominatorDf <= 0.- Since:
- 2.2
-
nextGamma
Deprecated.Generates a random value from the
Gamma Distribution.This implementation uses the following algorithms:
For 0 invalid input: '<' shape invalid input: '<' 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.- Parameters:
shape- the median of the Gamma distributionscale- the scale parameter of the Gamma distribution- Returns:
- random value sampled from the Gamma(shape, scale) distribution
- Throws:
NotStrictlyPositiveException- ifshape <= 0orscale <= 0.- Since:
- 2.2
-
nextHypergeometric
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException Deprecated.Generates a random value from theHypergeometric Distribution. This implementation usesinversionto generate random values.- Parameters:
populationSize- the population size of the Hypergeometric distributionnumberOfSuccesses- number of successes in the population of the Hypergeometric distributionsampleSize- the sample size of the Hypergeometric distribution- Returns:
- random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
- Throws:
NumberIsTooLargeException- ifnumberOfSuccesses > populationSize, orsampleSize > populationSize.NotStrictlyPositiveException- ifpopulationSize <= 0.NotPositiveException- ifnumberOfSuccesses < 0.- Since:
- 2.2
-
nextPascal
Deprecated.Generates a random value from thePascal Distribution. This implementation usesinversionto generate random values.- Parameters:
r- the number of successes of the Pascal distributionp- the probability of success of the Pascal distribution- Returns:
- random value sampled from the Pascal(r, p) distribution
- Throws:
NotStrictlyPositiveException- if the number of successes is not positiveOutOfRangeException- if the probability of success is not in the range[0, 1].- Since:
- 2.2
-
nextT
Deprecated.Generates a random value from theT Distribution. This implementation usesinversionto generate random values.- Parameters:
df- the degrees of freedom of the T distribution- Returns:
- random value from the T(df) distribution
- Throws:
NotStrictlyPositiveException- ifdf <= 0- Since:
- 2.2
-
nextWeibull
Deprecated.Generates a random value from theWeibull Distribution. This implementation usesinversionto generate random values.- Parameters:
shape- the shape parameter of the Weibull distributionscale- the scale parameter of the Weibull distribution- Returns:
- random value sampled from the Weibull(shape, size) distribution
- Throws:
NotStrictlyPositiveException- ifshape <= 0orscale <= 0.- Since:
- 2.2
-
nextZipf
Deprecated.Generates a random value from theZipf Distribution. This implementation usesinversionto generate random values.- Parameters:
numberOfElements- the number of elements of the ZipfDistributionexponent- the exponent of the ZipfDistribution- Returns:
- random value sampled from the Zipf(numberOfElements, exponent) distribution
- Throws:
NotStrictlyPositiveException- ifnumberOfElements <= 0orexponent <= 0.- Since:
- 2.2
-
reSeed
public void reSeed(long seed) Deprecated.Reseeds the random number generator with the supplied seed.Will create and initialize if null.
- Parameters:
seed- the seed value to use
-
reSeedSecure
public void reSeedSecure()Deprecated.Reseeds the secure random number generator with the current time in milliseconds.Will create and initialize if null.
-
reSeedSecure
public void reSeedSecure(long seed) Deprecated.Reseeds the secure random number generator with the supplied seed.Will create and initialize if null.
- Parameters:
seed- the seed value to use
-
reSeed
public void reSeed()Deprecated.Reseeds the random number generator withSystem.currentTimeMillis() + System.identityHashCode(this)). -
setSecureAlgorithm
public void setSecureAlgorithm(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException Deprecated.Sets the PRNG algorithm for the underlying SecureRandom instance using the Security Provider API. The Security Provider API is defined in Java Cryptography Architecture API Specification invalid input: '&' Reference.USAGE NOTE: This method carries significant overhead and may take several seconds to execute.
- Parameters:
algorithm- the name of the PRNG algorithmprovider- the name of the provider- Throws:
NoSuchAlgorithmException- if the specified algorithm is not availableNoSuchProviderException- if the specified provider is not installed
-
nextPermutation
public int[] nextPermutation(int n, int k) throws NotStrictlyPositiveException, NumberIsTooLargeException Deprecated.Generates an integer array of lengthkwhose entries are selected randomly, without repetition, from the integers0, ..., n - 1(inclusive).Generated arrays represent permutations of
ntakenkat a time.Uses a 2-cycle permutation shuffle. The shuffling process is described here.
- Specified by:
nextPermutationin interfaceRandomData- Parameters:
n- the domain of the permutationk- the size of the permutation- Returns:
- a random
k-permutation ofn, as an array of integers - Throws:
NotStrictlyPositiveException- ifk <= 0.NumberIsTooLargeException- ifk > n.
-
nextSample
public Object[] nextSample(Collection<?> c, int k) throws NotStrictlyPositiveException, NumberIsTooLargeException Deprecated.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.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- Specified by:
nextSamplein interfaceRandomData- Parameters:
c- the collection to be sampledk- the size of the sample- Returns:
- a random sample of
kelements fromc - Throws:
NotStrictlyPositiveException- ifk <= 0.NumberIsTooLargeException- ifk > c.size().
-
nextInversionDeviate
@Deprecated public double nextInversionDeviate(RealDistribution distribution) throws MathIllegalArgumentException Deprecated.use the distribution's sample() methodGenerate a random deviate from the given distribution using the inversion method.- Parameters:
distribution- Continuous distribution to generate a random value from- Returns:
- a random value sampled from the given distribution
- Throws:
MathIllegalArgumentException- if the underlynig distribution throws one- Since:
- 2.2
-
nextInversionDeviate
@Deprecated public int nextInversionDeviate(IntegerDistribution distribution) throws MathIllegalArgumentException Deprecated.use the distribution's sample() methodGenerate a random deviate from the given distribution using the inversion method.- Parameters:
distribution- Integer distribution to generate a random value from- Returns:
- a random value sampled from the given distribution
- Throws:
MathIllegalArgumentException- if the underlynig distribution throws one- Since:
- 2.2
-
RandomDataGeneratorinstead