Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.commons.math.random
Class RandomDataImpl

    java.lang.Object
      org.apache.commons.math.random.RandomDataImpl

All Implemented Interfaces:
  RandomData, Serializable

public class RandomDataImpl
extends Object
implements RandomData, Serializable

Implements the `RandomData` interface using `Random` and `Random.SecureRandom` instances to generate data.

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 PRNGs, see `Random` and `Random.SecureRandom`.

Usage Notes:

- Instance variables are used to maintain `Random` 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.
- 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 `RandomDataImpl` is created, the underlying random number generators are not intialized. The first call to a data generation method, or to a `reSeed()` method instantiates the appropriate generator. If you do not explicitly seed the generator, it is by default seeded with the current time in milliseconds
- The `reSeed` and `reSeedSecure` methods delegate to the corresponding methods on the underlying `Random` and `SecureRandom` instances. Therefore, the contracts of these methods are as defined in the JDK documentation. In particular, `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.

Version:
  $Revision: 1.16 $ $Date: 2004/07/22 02:34:25 $
See Also:
  Serialized Form

Field Summary
`(package private) static long` | `serialVersionUID` Serializable version identifier

Constructor Summary
`RandomDataImpl()` Construct a RandomDataImpl.

Method Summary
`double` | `nextExponential(double mean)` Returns a random value from an Exponential distribution with the given mean.
`double` | `nextGaussian(double mu, double sigma)` Generate a random value from a Normal (a.k.a.
`String` | `nextHexString(int len)` Algorithm Description: hex strings are generated using a 2-step process.
`int` | `nextInt(int lower, int upper)` Generate a random int value uniformly distributed between `lower` and `upper`, inclusive.
`long` | `nextLong(long lower, long upper)` Generate a random long value uniformly distributed between `lower` and `upper`, inclusive.
`int[]` | `nextPermutation(int n, int k)` Uses a 2-cycle permutation shuffle to generate a random permutation.
`long` | `nextPoisson(double mean)` Generates a random long value from the Poisson distribution with the given mean.
`Object[]` | `nextSample(Collection c, int k)` Uses a 2-cycle permutation shuffle to generate a random permutation.
`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)` 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` | `nextUniform(double lower, double upper)` Algorithm Description: scales the output of Random.nextDouble(), but rejects 0 values (i.e., will generate another random double if Random.nextDouble() returns 0).
`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(String algorithm, String provider)` Sets the PRNG algorithm for the underlying SecureRandom instance using the Security Provider API.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### serialVersionUID

    static final long serialVersionUID

  Serializable version identifier

  See Also:
    Constant Field Values

Constructor Detail

### RandomDataImpl

    public RandomDataImpl()

  Construct a RandomDataImpl.

Method Detail

### nextHexString

    public String nextHexString(int len)

  Algorithm Description: hex strings are generated using a 2-step process.
  1. len/2+1 binary bytes are generated using the underlying Random
  2. Each binary byte is translated into 2 hex digits

  Specified by:
    `nextHexString` in interface `RandomData`
  Parameters:
    `len` - the desired string length.
  Returns:
    the random string.

### nextInt

    public int nextInt(int lower,
                       int upper)

  Generate a random int value uniformly distributed between `lower` and `upper`, inclusive.

  Specified by:
    `nextInt` in interface `RandomData`
  Parameters:
    `lower` - the lower bound.
    `upper` - the upper bound.
  Returns:
    the random integer.

### nextLong

    public long nextLong(long lower,
                         long upper)

  Generate a random long value uniformly distributed between `lower` and `upper`, inclusive.

  Specified by:
    `nextLong` in interface `RandomData`
  Parameters:
    `lower` - the lower bound.
    `upper` - the upper bound.
  Returns:
    the random integer.

### nextSecureHexString

    public String nextSecureHexString(int len)

  Algorithm Description: hex strings are generated in 40-byte segments using a 3-step process.
  1. 20 random bytes are generated using the underlying `SecureRandom`.
  2. SHA-1 hash is applied to yield a 20-byte binary digest.
  3. Each byte of the binary digest is converted to 2 hex digits.

  Specified by:
    `nextSecureHexString` in interface `RandomData`
  Parameters:
    `len` - the length of the generated string
  Returns:
    the random string

### nextSecureInt

    public int nextSecureInt(int lower,
                             int upper)

  Generate a random int value uniformly distributed between `lower` and `upper`, inclusive. This algorithm uses a secure random number generator.

  Specified by:
    `nextSecureInt` in interface `RandomData`
  Parameters:
    `lower` - the lower bound.
    `upper` - the upper bound.
  Returns:
    the random integer.

### nextSecureLong

    public long nextSecureLong(long lower,
                               long upper)

  Generate a random long value uniformly distributed between `lower` and `upper`, inclusive. This algorithm uses a secure random number generator.

  Specified by:
    `nextSecureLong` in interface `RandomData`
  Parameters:
    `lower` - the lower bound.
    `upper` - the upper bound.
  Returns:
    the random integer.

### nextPoisson

    public long nextPoisson(double mean)

  Generates a random long value from the Poisson distribution with the given mean.

  Algorithm Description: Uses simulation of a Poisson process using Uniform deviates, as described here.

  The Poisson process (and hence value returned) is bounded by 1000 * mean.

  Specified by:
    `nextPoisson` in interface `RandomData`
  Parameters:
    `mean` - mean of the Poisson distribution.
  Returns:
    the random Poisson value.

### nextGaussian

    public double nextGaussian(double mu,
                               double sigma)

  Generate a random value from a Normal (a.k.a. Gaussian) distribution with the given mean, `mu` and the given standard deviation, `sigma`.

  Specified by:
    `nextGaussian` in interface `RandomData`
  Parameters:
    `mu` - the mean of the distribution
    `sigma` - the standard deviation of the distribution
  Returns:
    the random Normal value

### nextExponential

    public double nextExponential(double mean)

  Returns a random value from an Exponential distribution with the given mean.

  Algorithm Description: Uses the Inversion Method to generate exponentially distributed random values from uniform deviates.

  Specified by:
    `nextExponential` in interface `RandomData`
  Parameters:
    `mean` - the mean of the distribution
  Returns:
    the random Exponential value

### nextUniform

    public double nextUniform(double lower,
                              double upper)

  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:
    `nextUniform` in interface `RandomData`
  Parameters:
    `lower` - the lower bound.
    `upper` - the upper bound.
  Returns:
    the random value.

### reSeed

    public void reSeed(long seed)

  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()

  Reseeds the secure random number generator with the current time in milliseconds.

  Will create and initialize if null.

### reSeedSecure

    public void reSeedSecure(long seed)

  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()

  Reseeds the random number generator with the current time in milliseconds.

### setSecureAlgorithm

    public void setSecureAlgorithm(String algorithm,
                                   String provider)
                            throws NoSuchAlgorithmException,
                                   NoSuchProviderException

  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 & Reference.

  USAGE NOTE: This method carries significant overhead and may take several seconds to execute.

  Parameters:
    `algorithm` - the name of the PRNG algorithm
    `provider` - the name of the provider
  Throws:
    `NoSuchAlgorithmException` - if the specified algorithm is not available
    `NoSuchProviderException` - if the specified provider is not installed

### nextPermutation

    public int[] nextPermutation(int n,
                                 int k)

  Uses a 2-cycle permutation shuffle to generate a random permutation. The shuffling process is described here.

  Specified by:
    `nextPermutation` in interface `RandomData`
  Parameters:
    `n` - the population size.
    `k` - the number to choose.
  Returns:
    the random permutation.

### nextSample

    public Object[] nextSample(Collection c,
                               int k)

  Uses a 2-cycle permutation shuffle to generate a random permutation. 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:
    `nextSample` in interface `RandomData`
  Parameters:
    `c` - Collection to sample from.
    `k` - sample size.
  Returns:
    the random sample.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2003-2004 The Apache Software Foundation. All Rights Reserved.