Package org.apache.commons.rng.simple
Class JDKRandomWrapper
- java.lang.Object
-
- org.apache.commons.rng.simple.JDKRandomWrapper
-
- All Implemented Interfaces:
UniformRandomProvider
public final class JDKRandomWrapper extends Object implements UniformRandomProvider
Wraps aRandom
instance to implementUniformRandomProvider
. All methods from theRandom
that match those inUniformRandomProvider
are used directly.This class can be used to wrap an instance of
SecureRandom
. TheSecureRandom
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 (seeRandomSource
).- Since:
- 1.3
- See Also:
SecureRandom
,RandomSource
-
-
Constructor Summary
Constructors Constructor Description JDKRandomWrapper(Random rng)
Create a wrapper around a Random instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
nextBoolean()
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
nextDouble()
Generates adouble
value between 0 (inclusive) and 1 (exclusive).float
nextFloat()
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).
-
-
-
Constructor Detail
-
JDKRandomWrapper
public JDKRandomWrapper(Random rng)
Create a wrapper around a Random instance.- Parameters:
rng
- JDKRandom
instance to which the random number generation is delegated.
-
-
Method Detail
-
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).
-
-