org.apache.commons.math4.random

## Class RandomUtils.DataGenerator

• Enclosing class:
RandomUtils

public static class RandomUtils.DataGenerator
extends Object
Various random data generation routines.
• ### Method Summary

All Methods
Modifier and Type Method and Description
String nextHexString(int len, boolean useSha1)
Generates a random string of hex characters of length len.
long nextLong(long lower, long upper)
Generates a uniformly distributed random long integer between lower and upper (endpoints included).
int[] nextPermutation(int n, int k)
Generates an integer array of length k whose entries are selected randomly, without repetition, from the integers 0, ..., n - 1 (inclusive).
<T> List<T> nextSample(Collection<T> collection, int k)
Returns a list of k objects selected randomly from the given collection.
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).
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Method Detail

• #### nextHexString

public String nextHexString(int len,
boolean useSha1)
Generates a random string of hex characters of length len. Algorithm Description: how hexadecimal strings are generated depends on the value of the useSha1 argument.
• If useSha1 == false, a 2-step process is used:
1. len / 2 + 1 binary bytes are generated using the underlying generator.
2. Each binary byte is translated into 2 hex digits.
• If useSha1 == true, hex strings are generated in 40-byte segments using a 3-step process:
1. 20 random bytes are generated using the underlying generator.
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.
Parameters:
len - Length of the generated string.
useSha1 - Whether to use a digest. If true (resp. false), the 3-step (resp. 2-step) process will be used.
Returns:
the random string.
Throws:
NotStrictlyPositiveException - if len <= 0.
• #### nextLong

public long nextLong(long lower,
long upper)
Generates a uniformly distributed random long integer between lower and upper (endpoints included).
Parameters:
lower - Lower bound for generated long integer.
upper - Upper bound for generated long integer.
Returns:
a random long integer greater than or equal to lower and less than or equal to upper
Throws:
NumberIsTooLargeException - if lower >= upper
• #### nextUniform

public double nextUniform(double lower,
double upper)
Generates a uniformly distributed random value from the open interval (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).

Parameters:
lower - Lower bound of the support (excluded).
upper - Upper bound of the support (excluded).
Returns:
a uniformly distributed random value between lower and upper (both excluded).
Throws:
NumberIsTooLargeException - if lower >= upper.
NotFiniteNumberException - if one of the bounds is infinite.
NotANumberException - if one of the bounds is NaN.
• #### nextUniform

public double nextUniform(double lower,
double upper,
boolean lowerInclusive)
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 lower and upper - lower are the location and scale parameters, respectively.

Algorithm Description: if the lower bound is excluded, scales the output of "nextDouble()", but rejects 0 values (i.e. it will generate another random double if "nextDouble()" returns 0). This is necessary to provide a symmetric output interval (both endpoints excluded).

Parameters:
lower - Lower bound of the support.
upper - Exclusive upper bound of the support.
lowerInclusive - true if the lower bound is inclusive.
Returns:
a uniformly distributed random value in the (lower, upper) interval, if lowerInclusive is false, or in the [lower, upper) interval, if lowerInclusive is true.
Throws:
NumberIsTooLargeException - if lower >= upper.
NotFiniteNumberException - if one of the bounds is infinite.
NotANumberException - if one of the bounds is NaN.
• #### nextPermutation

public int[] nextPermutation(int n,
int k)
throws NumberIsTooLargeException,
NotStrictlyPositiveException
Generates an integer array of length 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.

This method calls MathArrays.shuffle in order to create a random shuffle of the set of natural numbers { 0, 1, ..., n - 1 }.

Parameters:
n - Domain of the permutation.
k - Size of the permutation.
Returns:
a random k-permutation of n, as an array of integers.
Throws:
NumberIsTooLargeException - if k > n.
NotStrictlyPositiveException - if k <= 0.
• #### nextSample

public <T> List<T> nextSample(Collection<T> collection,
int k)
Returns a list of k objects selected randomly from the given collection.

Sampling is without replacement; but if collection contains identical objects, the sample may include repeats. If all elements are distinct, the resulting object array represents a Simple Random Sample of size k from the elements of the collection.

This method calls nextPermutation(c.size(), k) in order to sample the collection.

Type Parameters:
T - Type of objects held in the collection.
Parameters:
collection - Collection to be sampled.
k - Size of the sample.
Returns:
a random sample of k elements from the collection.
Throws:
NumberIsTooLargeException - if k > collection.size().
NotStrictlyPositiveException - if k <= 0.