Package org.apache.commons.rng.simple
Class JDKRandomWrapper
java.lang.Object
org.apache.commons.rng.simple.JDKRandomWrapper
- All Implemented Interfaces:
UniformRandomProvider
Wraps a
Random
instance to implement UniformRandomProvider
. All methods from
the Random
that match those in UniformRandomProvider
are used directly.
This class can be used to wrap an instance of
SecureRandom
. The SecureRandom
class provides
cryptographic random number generation. The features available depend on the Java version
and platform. Consult the Java documentation for more details.
Note: Use of java.util.Random
is not recommended for applications.
There are many other pseudo-random number generators that are statistically superior and often
faster (see RandomSource
).
- Since:
- 1.3
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Generates aboolean
value.void
nextBytes
(byte[] bytes) Generatesbyte
values and places them into a user-supplied array.void
nextBytes
(byte[] bytes, int start, int len) Generatesbyte
values and places them into a user-supplied array.double
Generates adouble
value between 0 (inclusive) and 1 (exclusive).float
Generates afloat
value between 0 (inclusive) and 1 (exclusive).int
nextInt()
Generates anint
value.int
nextInt
(int n) Generates anint
value between 0 (inclusive) and the specified value (exclusive).long
nextLong()
Generates along
value.long
nextLong
(long n) Generates along
value between 0 (inclusive) and the specified value (exclusive).
-
Field Details
-
rng
The JDK Random instance.
-
-
Constructor Details
-
JDKRandomWrapper
Create a wrapper around a Random instance.- Parameters:
rng
- JDKRandom
instance to which the random number generation is delegated.
-
-
Method Details
-
nextBytes
public void nextBytes(byte[] bytes) Generatesbyte
values and places them into a user-supplied array.The number of random bytes produced is equal to the length of the byte array.
- Specified by:
nextBytes
in interfaceUniformRandomProvider
- Parameters:
bytes
- Byte array in which to put the random bytes. Cannot benull
.
-
nextBytes
public void nextBytes(byte[] bytes, int start, int len) Generatesbyte
values and places them into a user-supplied array.The array is filled with bytes extracted from random integers. This implies that the number of random bytes generated may be larger than the length of the byte array.
- Specified by:
nextBytes
in interfaceUniformRandomProvider
- Parameters:
bytes
- Array in which to put the generated bytes. Cannot benull
.start
- Index at which to start inserting the generated bytes.len
- Number of bytes to insert.
-
nextInt
public int nextInt()Generates anint
value.- Specified by:
nextInt
in interfaceUniformRandomProvider
- Returns:
- the next random value.
-
nextInt
public int nextInt(int n) Generates anint
value between 0 (inclusive) and the specified value (exclusive).- Specified by:
nextInt
in interfaceUniformRandomProvider
- Parameters:
n
- Bound on the random number to be returned. Must be positive.- Returns:
- a random
int
value between 0 (inclusive) andn
(exclusive).
-
nextLong
public long nextLong()Generates along
value.- Specified by:
nextLong
in interfaceUniformRandomProvider
- Returns:
- the next random value.
-
nextLong
public long nextLong(long n) Generates along
value between 0 (inclusive) and the specified value (exclusive).- Specified by:
nextLong
in interfaceUniformRandomProvider
- Parameters:
n
- Bound on the random number to be returned. Must be positive.- Returns:
- a random
long
value between 0 (inclusive) andn
(exclusive).
-
nextBoolean
public boolean nextBoolean()Generates aboolean
value.- Specified by:
nextBoolean
in interfaceUniformRandomProvider
- Returns:
- the next random value.
-
nextFloat
public float nextFloat()Generates afloat
value between 0 (inclusive) and 1 (exclusive).- Specified by:
nextFloat
in interfaceUniformRandomProvider
- Returns:
- the next random value between 0 (inclusive) and 1 (exclusive).
-
nextDouble
public double nextDouble()Generates adouble
value between 0 (inclusive) and 1 (exclusive).- Specified by:
nextDouble
in interfaceUniformRandomProvider
- Returns:
- the next random value between 0 (inclusive) and 1 (exclusive).
-