public static final class math.random extends StaticObject
Modifier and Type | Method and Description |
---|---|
static double |
nextDouble(Random random,
double min,
double max)
Returns a pseudo-random, uniformly distributed double value between
min (inclusively) and max (exclusively).
|
static float |
nextFloat(Random random,
float min,
float max)
Returns a pseudo-random, uniformly distributed double value between
min (inclusively) and max (exclusively).
|
static int |
nextInt(Random random,
int min,
int max)
Returns a pseudo-random, uniformly distributed int value between min
and max (min and max included).
|
static long |
nextLong(Random random,
long n)
Returns a pseudo-random, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive), drawn from the given
random number generator's sequence.
|
static long |
nextLong(Random random,
long min,
long max)
Returns a pseudo-random, uniformly distributed int value between min
and max (min and max included).
|
static long |
seed()
Calculating a 64 bit seed value which can be used for initializing
PRNGs.
|
static byte[] |
seed(byte[] seed)
Fills the given byte array with random bytes, created by successive
calls of the
seed() method. |
static long |
seed(long base)
Uses the given
base value to create a reasonable safe seed
value. |
static byte[] |
seedBytes(int length)
Create a new seed byte array of the given length.
|
public static int nextInt(Random random, int min, int max)
random
- the random engine to use for calculating the random
int valuemin
- lower bound for generated integermax
- upper bound for generated integermin
and
less than or equal to max
IllegalArgumentException
- if min >= max
public static long nextLong(Random random, long min, long max)
random
- the random engine to use for calculating the random
long valuemin
- lower bound for generated long integermax
- upper bound for generated long integermin
and less than or equal to max
IllegalArgumentException
- if min >= max
public static long nextLong(Random random, long n)
random
- the random engine used for creating the random number.n
- the bound on the random number to be returned. Must be
positive.IllegalArgumentException
- if n is smaller than 1.public static float nextFloat(Random random, float min, float max)
random
- the random engine used for creating the random number.min
- lower bound for generated float valuemax
- upper bound for generated float valuemin
and less
than to max
public static double nextDouble(Random random, double min, double max)
random
- the random engine used for creating the random number.min
- lower bound for generated double valuemax
- upper bound for generated double valuemin
and less
than to max
public static byte[] seedBytes(int length)
length
- the length of the returned byte array.NegativeArraySizeException
- if the given length is smaller
than zero.seed(byte[])
,
seed()
public static byte[] seed(byte[] seed)
seed()
method.seed
- the byte array seed to fill with random bytes.NullPointerException
- if the seed
array is
null
.seed()
public static long seed()
System.nanoTime()
and new Object().hashCode()
calls to create a reasonable safe
seed value:
public static long seed() {
return seed(System.nanoTime());
}
This method passes all of the statistical tests of the dieharder test suite—executed on a linux machine with JDK version 1.7. Since there is no prove that this will the case for every Java version and OS, it is recommended to only use this method for seeding other PRNGs.
seed(long)
public static long seed(long base)
base
value to create a reasonable safe seed
value. This is done by combining it with values of
new Object().hashCode()
:
public static long seed(final long base) {
final long objectHashSeed = ((long)(new Object().hashCode()) << 32) |
new Object().hashCode();
long seed = base ^ objectHashSeed;
seed ^= seed << 17;
seed ^= seed >>> 31;
seed ^= seed << 8;
return seed;
}
base
- the base value of the seed to create© 2007-2014 Franz Wilhelmstötter (2014-10-03 19:44)