public class TestableRandom extends Random
Random
adds extra methods useful
for testing purposes. Normally, you might generate a new random number
by calling nextInt()
, nextDouble()
, or one of the
other generation methods provided by Random
. Normally,
this intentionally makes your code behave in a random way, which may
make it harder to test. This class allows you to control directly
the sequence of numbers that will be generated by such calls.
Suppose your code is written this way:
Random random = new TestableRandom(); ... int x = random.nextInt(64);
You can then write test cases that look like this:
public void testSomeFeature() { // Set the return values for the next 6 calls to nextInt(), // No matter which instance of TestableRandom the method is called on TestableRandom.setNextInts(5, 10, 22, 13, 12, 47); // Perform tests, knowing in advance the exact sequence of numbers // That will now be generated }
This class provides separate methods to preset the sequence of booleans, ints, doubles, floats, bytes, or Gaussian-distributed doubles that will be generated. You can pass in as many specific values to the setNext...() methods that you like, or you can even pass in an array:
int[] someValues = new int[] { 1, 2, 3, 4, 5, 6, 7 }; TestableRandom.setNextInts(someValues);
Constructor and Description |
---|
TestableRandom()
Creates a new random number generator.
|
TestableRandom(long seed)
Creates a new random number generator using a single long seed.
|
Modifier and Type | Method and Description |
---|---|
boolean |
nextBoolean()
Returns the next pseudorandom, uniformly distributed
boolean value from this random number generator's
sequence. |
void |
nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied
byte array.
|
double |
nextDouble()
Returns the next pseudorandom, uniformly distributed
double value between 0.0 and
1.0 from this random number generator's sequence. |
double |
nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributed
double value with mean 0.0 and standard
deviation 1.0 from this random number generator's sequence. |
int |
nextInt()
Returns the next pseudorandom, uniformly distributed
int
value from this random number generator's sequence. |
int |
nextInt(int n)
Returns a pseudorandom, uniformly distributed
int value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence. |
long |
nextLong()
Returns the next pseudorandom, uniformly distributed
long
value from this random number generator's sequence. |
static void |
setNextBooleans(boolean... values)
This method allows one to provide a predefined series of values that
will override the results provided by
nextBoolean() . |
static void |
setNextBytes(byte... values)
This method allows one to provide a predefined series of values that
will override the results provided by
nextBytes(byte[]) . |
static void |
setNextDoubles(double... values)
This method allows one to provide a predefined series of values that
will override the results provided by
nextDouble() . |
static void |
setNextFloats(float... values)
This method allows one to provide a predefined series of values that
will override the results provided by
Random.nextFloat() . |
static void |
setNextGaussians(double... values)
This method allows one to provide a predefined series of values that
will override the results provided by
nextGaussian() . |
static void |
setNextInts(int... values)
This method allows one to provide a predefined series of values that
will override the results provided by
nextInt() and
nextInt(int) . |
static void |
setNextLongs(long... values)
This method allows one to provide a predefined series of values that
will override the results provided by
nextLong() . |
public TestableRandom()
public TestableRandom(long seed)
Random.next(int)
.
The invocation new Random(seed) is equivalent to:
Random rnd = new Random(); rnd.setSeed(seed);
seed
- the initial seedRandom.setSeed(long)
public static void setNextInts(int... values)
nextInt()
and
nextInt(int)
. This is useful during testing, when you want
to control the results generated by a random number generator. If
you do not use this method, nextInt()
and nextInt(int)
behave normally.
Note that the sequence of int values you provide will be shared by all instances of this class--so, no matter how many TestableRandom instances you have created, the sequence of random numbers generated by calls to their methods will be determined by what you pass in here.
If previous values from an earlier call to this method have not yet been used, they will be replaced by any parameters you provide in the next call to this method. If previous values from an earlier call to this method have not yet been used, and you provide no arguments in your next call to this method, those unused values will be replaced with nothing, so normal pseudorandom generation behavior will resume immediately.
values
- a sequence of int values to use as the results in
subsequent calls to nextInt()
or nextInt(int)
public static void setNextLongs(long... values)
nextLong()
. This is
useful during testing, when you want to control the results generated
by a random number generator. If you do not use this method,
nextLong()
behaves normally.
Note that the sequence of long values you provide will be shared by all instances of this class--so, no matter how many TestableRandom instances you have created, the sequence of random numbers generated by calls to their methods will be determined by what you pass in here.
If previous values from an earlier call to this method have not yet been used, they will be replaced by any parameters you provide in the next call to this method. If previous values from an earlier call to this method have not yet been used, and you provide no arguments in your next call to this method, those unused values will be replaced with nothing, so normal pseudorandom generation behavior will resume immediately.
values
- a sequence of long values to use as the results in
subsequent calls to nextLong()
public static void setNextBooleans(boolean... values)
nextBoolean()
. This is
useful during testing, when you want to control the results generated
by a random number generator. If you do not use this method,
nextBoolean()
behaves normally.
Note that the sequence of boolean values you provide will be shared by all instances of this class--so, no matter how many TestableRandom instances you have created, the sequence of random booleans generated by calls to their methods will be determined by what you pass in here.
If previous values from an earlier call to this method have not yet been used, they will be replaced by any parameters you provide in the next call to this method. If previous values from an earlier call to this method have not yet been used, and you provide no arguments in your next call to this method, those unused values will be replaced with nothing, so normal pseudorandom generation behavior will resume immediately.
values
- a sequence of boolean values to use as the results in
subsequent calls to nextBoolean()
public static void setNextFloats(float... values)
Random.nextFloat()
. This is
useful during testing, when you want to control the results generated
by a random number generator. If you do not use this method,
Random.nextFloat()
behaves normally.
Note that the sequence of float values you provide will be shared by all instances of this class--so, no matter how many TestableRandom instances you have created, the sequence of random numbers generated by calls to their methods will be determined by what you pass in here.
If previous values from an earlier call to this method have not yet been used, they will be replaced by any parameters you provide in the next call to this method. If previous values from an earlier call to this method have not yet been used, and you provide no arguments in your next call to this method, those unused values will be replaced with nothing, so normal pseudorandom generation behavior will resume immediately.
values
- a sequence of float values to use as the results in
subsequent calls to Random.nextFloat()
public static void setNextDoubles(double... values)
nextDouble()
. This is
useful during testing, when you want to control the results generated
by a random number generator. If you do not use this method,
nextDouble()
behaves normally.
Note that the sequence of double values you provide will be shared by all instances of this class--so, no matter how many TestableRandom instances you have created, the sequence of random numbers generated by calls to their methods will be determined by what you pass in here.
If previous values from an earlier call to this method have not yet been used, they will be replaced by any parameters you provide in the next call to this method. If previous values from an earlier call to this method have not yet been used, and you provide no arguments in your next call to this method, those unused values will be replaced with nothing, so normal pseudorandom generation behavior will resume immediately.
values
- a sequence of double values to use as the results in
subsequent calls to nextDouble()
public static void setNextGaussians(double... values)
nextGaussian()
. This is
useful during testing, when you want to control the results generated
by a random number generator. If you do not use this method,
nextGaussian()
behaves normally.
Note that the sequence of double values you provide will be shared by all instances of this class--so, no matter how many TestableRandom instances you have created, the sequence of random numbers generated by calls to their methods will be determined by what you pass in here.
If previous values from an earlier call to this method have not yet been used, they will be replaced by any parameters you provide in the next call to this method. If previous values from an earlier call to this method have not yet been used, and you provide no arguments in your next call to this method, those unused values will be replaced with nothing, so normal pseudorandom generation behavior will resume immediately.
values
- a sequence of double values to use as the results in
subsequent calls to nextGaussian()
public static void setNextBytes(byte... values)
nextBytes(byte[])
.
This is useful during testing, when you want to control the results
generated by a random number generator. If you do not use this method,
nextBytes(byte[])
behaves normally.
Note that the sequence of byte values you provide will be shared by all instances of this class--so, no matter how many TestableRandom instances you have created, the sequence of random numbers generated by calls to their methods will be determined by what you pass in here.
If previous values from an earlier call to this method have not yet been used, they will be replaced by any parameters you provide in the next call to this method. If previous values from an earlier call to this method have not yet been used, and you provide no arguments in your next call to this method, those unused values will be replaced with nothing, so normal pseudorandom generation behavior will resume immediately.
values
- a sequence of byte values to use as the results in
subsequent calls to nextBytes(byte[])
public int nextInt()
int
value from this random number generator's sequence. The general
contract of nextInt
is that one int
value is
pseudorandomly generated and returned. All 232
possible int
values are produced with
(approximately) equal probability.
If setNextInts(int...)
has been called, the next available
value from the provided sequence will be returned until that sequence
is exhausted. One all provided values have been returned, then
true pseudorandom generation will resume.
public int nextInt(int n)
int
value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence. The general contract of
nextInt
is that one int
value in the specified range
is pseudorandomly generated and returned. All n
possible
int
values are produced with (approximately) equal
probability.
If setNextInts(int...)
has been called, the next available
value from the provided sequence (modulo n) will be returned until that
sequence is exhausted. One all provided values have been returned, then
true pseudorandom generation will resume.
nextInt
in class Random
n
- the bound on the random number to be returned. Must be
positive.int
value between 0
(inclusive) and n
(exclusive)
from this random number generator's sequenceIllegalArgumentException
- if n is not positivepublic long nextLong()
long
value from this random number generator's sequence. The general
contract of nextLong
is that one long
value is
pseudorandomly generated and returned.
If setNextLongs(long...)
has been called, the next available
value from the provided sequence will be returned until that sequence
is exhausted. One all provided values have been returned, then
true pseudorandom generation will resume.
public boolean nextBoolean()
boolean
value from this random number generator's
sequence. The general contract of nextBoolean
is that one
boolean
value is pseudorandomly generated and returned. The
values true
and false
are produced with
(approximately) equal probability.
If setNextBooleans(boolean...)
has been called, the next
available value from the provided sequence will be returned until that
sequence is exhausted. One all provided values have been returned, then
true pseudorandom generation will resume.
nextBoolean
in class Random
boolean
value from this random number generator's sequencepublic double nextDouble()
double
value between 0.0
and
1.0
from this random number generator's sequence.
If setNextDoubles(double...)
has been called, the next
available value from the provided sequence will be returned until that
sequence is exhausted. One all provided values have been returned, then
true pseudorandom generation will resume.
nextDouble
in class Random
double
value from this random number generator's sequencepublic double nextGaussian()
double
value with mean 0.0
and standard
deviation 1.0
from this random number generator's sequence.
If setNextGaussians(double...)
has been called, the next
available value from the provided sequence will be returned until that
sequence is exhausted. One all provided values have been returned, then
true pseudorandom generation will resume.
nextGaussian
in class Random
double
value with mean 0.0
and
standard deviation 1.0
from this random number
generator's sequencepublic void nextBytes(byte[] bytes)
If setNextBytes(byte...)
has been called, unused
values from the provided sequence will be used to fill the provided
array until that sequence is exhausted. One all provided values have
been used up, true pseudorandom generation will resume for filling
any remaining slots in this or future calls.
nextBytes
in class Random
bytes
- the byte array to fill with random bytesNullPointerException
- if the byte array is null