public class RandomDataGenerator extends Object implements RandomData, 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 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 rand)Construct a RandomDataGenerator using the supplied  RandomGeneratoras
 the source of (non-secure) random data. | 
| Modifier and Type | Method and Description | 
|---|---|
| RandomGenerator | getRandomGenerator()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)Generates a random value from the exponential distribution
 with specified 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)Generates a random value from the Normal (or Gaussian) distribution with
 specified mean and standard deviation. | 
| 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)Generates a uniformly distributed random integer between  lowerandupper(endpoints included). | 
| long | nextLong(long lower,
        long upper)Generates a uniformly distributed random long integer between
  lowerandupper(endpoints included). | 
| 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  kwhose entries are selected
 randomly, without repetition, from the integers0, ..., n - 1(inclusive). | 
| long | nextPoisson(double mean)Generates a random value from the Poisson distribution with the given
 mean. | 
| Object[] | nextSample(Collection<?> c,
          int k)Returns an array of  kobjects selected randomly from the
 Collectionc. | 
| String | nextSecureHexString(int len)Generates a random string of hex characters from a secure random
 sequence. | 
| int | nextSecureInt(int lower,
             int upper)Generates a uniformly distributed random integer between  lowerandupper(endpoints included) from a secure random sequence. | 
| long | nextSecureLong(long lower,
              long upper)Generates a uniformly distributed random long integer between
  lowerandupper(endpoints included) from a secure random
 sequence. | 
| 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 | nextUniform(double lower,
           double upper,
           boolean lowerInclusive)Generates a uniformly distributed random value from the interval
  (lower, upper)or the interval[lower, upper). | 
| 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 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 RandomDatalen - 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 RandomDatalower - lower bound for generated integerupper - upper bound for generated integerlower
 and less than or equal to upperNumberIsTooLargeException - if lower >= upperpublic 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 RandomDatalower - lower bound for generated long integerupper - upper bound for generated long integerlower and
 less than or equal to upperNumberIsTooLargeException - if lower >= upperpublic 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 RandomDatalen - the length of the string to be generatedlenNotStrictlyPositiveException - if len <= 0public 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 RandomDatalower - 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 RandomDatalower - 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 RandomDatamean - the mean of the Poisson distributionNotStrictlyPositiveException - if len <= 0public double nextGaussian(double mu,
                  double sigma)
                    throws NotStrictlyPositiveException
Definition: Normal Distribution
nextGaussian in interface RandomDatamu - 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 RandomDatamean - the mean of the distributionNotStrictlyPositiveException - if mean <= 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.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.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.df - the degrees of freedom of the T distributionNotStrictlyPositiveException - if df <= 0public double nextWeibull(double shape,
                 double scale)
                   throws NotStrictlyPositiveException
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)
             throws NotStrictlyPositiveException
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)
             throws NotStrictlyPositiveException
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)
                   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 RandomDatalower - the exclusive lower bound of the supportupper - the exclusive upper bound of the supportNumberIsTooLargeException - if lower >= upperNotFiniteNumberException - 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 RandomDatalower - 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
 trueNumberIsTooLargeException - if lower >= upperNotFiniteNumberException - if one of the bounds is infiniteNotANumberException - if one of the bounds is NaNpublic int[] nextPermutation(int n,
                    int k)
                      throws NumberIsTooLargeException,
                             NotStrictlyPositiveException
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.
MathArrays.shuffle in order to create a random shuffle of the set
 of natural numbers { 0, 1, ..., n - 1 }.nextPermutation in interface RandomDatan - the domain of the permutationk - the size of the permutationk-permutation of n, as an array of
 integersNumberIsTooLargeException - if k > n.NotStrictlyPositiveException - if k <= 0.public Object[] nextSample(Collection<?> c, int k) throws NumberIsTooLargeException, NotStrictlyPositiveException
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.
nextPermutation(c.size(), k)
 in order to sample the collection.nextSample in interface RandomDatac - the collection to be sampledk - the size of the samplek elements from cNumberIsTooLargeException - if k > c.size().NotStrictlyPositiveException - if k <= 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 RandomGenerator getRandomGenerator()
 Creates and initializes a default generator if null. Uses a Well19937c
 generator with System.currentTimeMillis() + System.identityHashCode(this))
 as the default seed.
 
Copyright © 2003–2016 The Apache Software Foundation. All rights reserved.