|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.math.random.RandomDataImpl
Implements the RandomData
interface using
java.util.Random
and
java.util.Random.SecureRandom
instances to generate data.
Supports reseeding the underlying
PRNG. The SecurityProvider
and Algorithm
used by the SecureRandom
instance can also be reset.
For details on the PRNGs, see the JDK documentation for
java.util.Random
and
java.util.Random.SecureRandom
Usage Notes:
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.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 millisecondsreSeed
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).
Constructor Summary | |
RandomDataImpl()
Construct a RandomDataImpl. |
Method Summary | |
double |
nextExponential(double mean)
Algorithm Description: Uses the Inversion Method to generate exponential from uniform deviates. |
double |
nextGaussian(double mu,
double sigma)
Generate a random value from a Normal distribution. |
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)
Algorithm Description: Uses simulation of a Poisson process using Uniform deviates, as described here |
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 |
Constructor Detail |
public RandomDataImpl()
Method Detail |
public String nextHexString(int len)
nextHexString
in interface RandomData
len
- the desired string length.
public int nextInt(int lower, int upper)
lower
and upper
, inclusive.
nextInt
in interface RandomData
lower
- the lower bound.upper
- the upper bound.
public long nextLong(long lower, long upper)
lower
and upper
, inclusive.
nextLong
in interface RandomData
lower
- the lower bound.upper
- the upper bound.
public String nextSecureHexString(int len)
SecureRandom
.TODO: find external reference or provide justification for the claim that this yields a cryptographically secure sequence of hex strings.
nextSecureHexString
in interface RandomData
len
- the desired string length.
public int nextSecureInt(int lower, int upper)
lower
and upper
, inclusive. This algorithm
using a secure random number generator for its engine.
nextSecureInt
in interface RandomData
lower
- the lower bound.upper
- the upper bound.
public long nextSecureLong(long lower, long upper)
lower
and upper
, inclusive. This algorithm
using a secure random number generator for its engine.
nextSecureLong
in interface RandomData
lower
- the lower bound.upper
- the upper bound.
public long nextPoisson(double mean)
nextPoisson
in interface RandomData
mean
- mean of the Poisson distribution.
public double nextGaussian(double mu, double sigma)
mu
and the given standard deviation,
sigma
.
nextGaussian
in interface RandomData
mu
- the mean of the distribution.sigma
- the standard deviation of the distribution.
public double nextExponential(double mean)
nextExponential
in interface RandomData
mean
- the mean of the distribution.
public double nextUniform(double lower, double upper)
nextUniform
in interface RandomData
lower
- the lower bound.upper
- the upper bound.
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()
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 provider
NoSuchAlgorithmException
- if the specified algorithm
is not available
NoSuchProviderException
- if the specified provider
is not installedpublic int[] nextPermutation(int n, int k)
nextPermutation
in interface RandomData
n
- the population size.k
- the number to choose.
public Object[] nextSample(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,
here
nextSample
in interface RandomData
c
- Collection to sample from.k
- sample size.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |