Class SourceOfRandomness
java.lang.Object
com.pholser.junit.quickcheck.random.SourceOfRandomness
A source of randomness, fed to
generators
so they can produce random values for property parameters.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Randomprivate static final BigIntegerprivate long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> Tchoose(Collection<T> items) Gives a random element of the given collection.<T> Tchoose(T[] items) Gives a random element of the given array.protected final Randomdelegate()Gives a reference to the JDK-random delegate of this instance.nextBigInteger(int numberOfBits) Gives a randomBigIntegerrepresentable by the given number of bits.booleanbytenextByte(byte min, byte max) Gives a randombytevalue, uniformly distributed across the interval[min, max].voidnextBytes(byte[] bytes) byte[]nextBytes(int count) Gives an array of a given length containing random bytes.charnextChar(char min, char max) Gives a randomcharvalue, uniformly distributed across the interval[min, max].doubledoublenextDouble(double min, double max) Gives a randomdoublevalue in the interval[min, max).nextDuration(Duration min, Duration max) Gives a randomDurationvalue, uniformly distributed across the interval[min, max].floatfloatnextFloat(float min, float max) Gives a randomfloatvalue in the interval[min, max).doublenextInstant(Instant min, Instant max) Gives a randomInstantvalue, uniformly distributed across the interval[min, max].intnextInt()intnextInt(int n) intnextInt(int min, int max) Gives a randomintvalue, uniformly distributed across the interval[min, max].longnextLong()longnextLong(long min, long max) Gives a randomlongvalue, uniformly distributed across the interval[min, max].private long[]nextSecondsAndNanos(long minSeconds, long minNanos, long maxSeconds, long maxNanos) shortnextShort(short min, short max) Gives a randomshortvalue, uniformly distributed across the interval[min, max].longseed()voidsetSeed(long seed) Gives a JDK source of randomness, with the same internal state as this source of randomness.
-
Field Details
-
NANOS_PER_SECOND
-
delegate
-
seed
private long seed
-
-
Constructor Details
-
SourceOfRandomness
Makes a new source of randomness.- Parameters:
delegate- a JDK source of randomness, to which the new instance will delegate
-
-
Method Details
-
toJDKRandom
Gives a JDK source of randomness, with the same internal state as this source of randomness.
- Returns:
- a JDK "clone" of self
-
nextBoolean
public boolean nextBoolean()- Returns:
- a uniformly distributed boolean value
- See Also:
-
nextBytes
public void nextBytes(byte[] bytes) - Parameters:
bytes- a byte array to fill with random values- See Also:
-
nextBytes
public byte[] nextBytes(int count) Gives an array of a given length containing random bytes.- Parameters:
count- the desired length of the random byte array- Returns:
- random bytes
- See Also:
-
nextDouble
public double nextDouble()- Returns:
- a uniformly distributed random
doublevalue in the interval[0.0, 1.0) - See Also:
-
nextFloat
public float nextFloat()- Returns:
- a uniformly distributed random
floatvalue in the interval[0.0, 1.0) - See Also:
-
nextGaussian
public double nextGaussian()- Returns:
- a Gaussian-distributed random double value
- See Also:
-
nextInt
public int nextInt()- Returns:
- a uniformly distributed random
intvalue - See Also:
-
nextInt
public int nextInt(int n) - Parameters:
n- upper bound- Returns:
- a uniformly distributed random
intvalue in the interval[0, n) - See Also:
-
nextLong
public long nextLong()- Returns:
- a uniformly distributed random
longvalue - See Also:
-
setSeed
public void setSeed(long seed) - Parameters:
seed- value with which to seed this source of randomness- See Also:
-
seed
public long seed()- Returns:
- the value used to initially seed this source of randomness
-
nextByte
public byte nextByte(byte min, byte max) Gives a randombytevalue, uniformly distributed across the interval[min, max].- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
nextChar
public char nextChar(char min, char max) Gives a randomcharvalue, uniformly distributed across the interval[min, max].- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
nextDouble
public double nextDouble(double min, double max) Gives a random
doublevalue in the interval[min, max).This naive implementation takes a random
doublevalue fromRandom.nextDouble()and scales/shifts the value into the desired interval. This may give surprising results for large ranges.- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
nextFloat
public float nextFloat(float min, float max) Gives a random
floatvalue in the interval[min, max).This naive implementation takes a random
floatvalue fromRandom.nextFloat()and scales/shifts the value into the desired interval. This may give surprising results for large ranges.- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
nextInt
public int nextInt(int min, int max) Gives a randomintvalue, uniformly distributed across the interval[min, max].- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
nextLong
public long nextLong(long min, long max) Gives a randomlongvalue, uniformly distributed across the interval[min, max].- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
nextShort
public short nextShort(short min, short max) Gives a randomshortvalue, uniformly distributed across the interval[min, max].- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
nextBigInteger
Gives a randomBigIntegerrepresentable by the given number of bits.- Parameters:
numberOfBits- the desired number of bits- Returns:
- a random
BigInteger - See Also:
-
nextInstant
Gives a randomInstantvalue, uniformly distributed across the interval[min, max].- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
nextDuration
Gives a randomDurationvalue, uniformly distributed across the interval[min, max].- Parameters:
min- lower bound of the desired intervalmax- upper bound of the desired interval- Returns:
- a random value
-
choose
Gives a random element of the given collection.- Type Parameters:
T- type of items in the collection- Parameters:
items- a collection- Returns:
- a randomly chosen element from the collection
-
choose
public <T> T choose(T[] items) Gives a random element of the given array.- Type Parameters:
T- type of items in the array- Parameters:
items- an array- Returns:
- a randomly chosen element from the array
-
delegate
Gives a reference to the JDK-random delegate of this instance. Intended for subclasses.- Returns:
- the JDK-random delegate
-
nextSecondsAndNanos
private long[] nextSecondsAndNanos(long minSeconds, long minNanos, long maxSeconds, long maxNanos)
-