public final class RandomUtils extends Random
RandomUtils
is a singleton class with more powerful random generating methods.
Useful methods include:
nextXXX()
for the byte and short types.
nextXXX(mod)
for all types.
nextXXX(min, max)
for all types.
pickRandom()
- picks a random element from a given List
or Set
.
Random
,
Serialized FormModifier and Type | Method and Description |
---|---|
long |
getSeed()
Returns the seed that was set last.
|
static RandomUtils |
instance()
Returns the single(ton) instance.
|
static RandomUtils |
instance(long seed)
Returns the single(ton) instance set with the given seed.
|
BigInteger |
nextBigInt(int numOfBits)
generates a new big integer with the desired number of bits the generated number will always be positive.
|
byte |
nextByte()
Randomizes a byte value.
|
byte |
nextByte(byte b)
Randomize a byte value between 0 (inclusive) and the specified value (exclusive).
|
byte |
nextByte(byte min,
byte max)
Randomize a byte value in the given range [min, max].
|
byte[] |
nextBytes(int size)
Creates a byte array of the specified size, initialized with random values.
|
double |
nextDouble(double d)
Randomize a double value between 0.0 (inclusive) and the specified value (exclusive).
|
double |
nextDouble(double d,
boolean inclusive)
Randomize a double value between 0.0 (inclusive) and the specified value (inclusive or exclusive as
required).
|
<T extends Enum<?>> |
nextEnum(Class<T> enumClass)
Returns a random value from an enum.
|
float |
nextFloat(float f)
Randomize a float value between 0.0 (inclusive) and the specified value (exclusive).
|
float |
nextFloat(float f,
boolean inclusive)
Randomize a float value between 0.0 (inclusive) and the specified value (inclusive or exclusive as
required).
|
float |
nextFloat(float min,
float max)
Randomize a float value in the given range [min, max].
|
int |
nextInt(int min,
int max)
Randomize an int value in the given range [min, max].
|
long |
nextLong(long l)
Randomize a long value between 0 (inclusive) and the specified value (exclusive).
|
long |
nextLong(long min,
long max)
Randomize a long value in the given range [min, max].
|
String |
nextNumericString(int length)
Randomize a valid numeric string.
|
short |
nextShort()
Randomizes a short value.
|
short |
nextShort(short s)
Randomize a short value between 0 (inclusive) and the specified value (exclusive).
|
short |
nextShort(short min,
short max)
Randomize a short value in the given range [min, max].
|
String |
nextString(int length)
Randomize a printable
String . |
String |
nextString(int length,
boolean printable)
Randomize a
String . |
String |
nextString(int min,
int max)
Randomize a printable
String of a length in the given range [min, max]. |
String |
nextString(int min,
int max,
boolean printable)
Randomize a
String of a length in the given range [min, max]. |
String |
nextXmlString(int length)
Randomize a valid XML Element name.
|
<T> T |
pickRandom(Collection<T> c)
Picks a random element from the given
Collection . |
<T> T |
pickRandom(T[] o)
Picks a random element from the given array.
|
void |
setSeed(long seed)
The last seed is saved, so it is possible to
getSeed() it later. |
next, nextBoolean, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong
public static RandomUtils instance()
public static RandomUtils instance(long seed)
public void setSeed(long seed)
getSeed()
it later. Since java.util.Random
's
seed is private, I am obliged to save my own copy.
See Random.setSeed(long)
.public long getSeed()
public byte nextByte()
public byte nextByte(byte b)
public byte nextByte(byte min, byte max)
public short nextShort()
public short nextShort(short s)
public short nextShort(short min, short max)
public int nextInt(int min, int max)
public long nextLong(long l)
public long nextLong(long min, long max)
public float nextFloat(float f)
public float nextFloat(float f, boolean inclusive)
inclusive
- Whether or not, the returned value should include the given one.public float nextFloat(float min, float max)
public double nextDouble(double d)
public double nextDouble(double d, boolean inclusive)
inclusive
- Whether or not, the returned value should include the given one.public <T> T pickRandom(Collection<T> c)
Collection
.public <T> T pickRandom(T[] o)
public String nextString(int length, boolean printable)
String
.length
- The requested length of the string.printable
- Whether or not, the string should contain only printable characters.public String nextNumericString(int length)
length
- The requested length of the string.public String nextXmlString(int length)
length
- The requested length of the string.public String nextString(int length)
String
.length
- The requested length of the string.public String nextString(int min, int max, boolean printable)
String
of a length in the given range [min, max].printable
- Whether or not, the string should contain only printable characters.public String nextString(int min, int max)
String
of a length in the given range [min, max].public byte[] nextBytes(int size)
public BigInteger nextBigInt(int numOfBits)
numOfBits
- the number of bits of the Big IntegerCopyright © 2012. All Rights Reserved.