Most visited

Recently visited

Added in API level 24

SplittableRandom

public final class SplittableRandom
extends Object

java.lang.Object
   ↳ java.util.SplittableRandom


A generator of uniform pseudorandom values applicable for use in (among other contexts) isolated parallel computations that may generate subtasks. Class SplittableRandom supports methods for producing pseudorandom numbers of type int, long, and double with similar usages as for class Random but differs in the following ways:

Instances of SplittableRandom are not cryptographically secure. Consider instead using SecureRandom in security-sensitive applications. Additionally, default-constructed instances do not use a cryptographically random seed unless the system property java.util.secureRandomSeed is set to true.

Summary

Public constructors

SplittableRandom(long seed)

Creates a new SplittableRandom instance using the specified initial seed.

SplittableRandom()

Creates a new SplittableRandom instance that is likely to generate sequences of values that are statistically independent of those of any other instances in the current program; and may, and typically does, vary across program invocations.

Public methods

DoubleStream doubles(long streamSize)

Returns a stream producing the given streamSize number of pseudorandom double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).

DoubleStream doubles()

Returns an effectively unlimited stream of pseudorandom double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).

DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)

Returns an effectively unlimited stream of pseudorandom double values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom double values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

IntStream ints(long streamSize)

Returns a stream producing the given streamSize number of pseudorandom int values from this generator and/or one split from it.

IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom int values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

IntStream ints(int randomNumberOrigin, int randomNumberBound)

Returns an effectively unlimited stream of pseudorandom int values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

IntStream ints()

Returns an effectively unlimited stream of pseudorandom int values from this generator and/or one split from it.

LongStream longs()

Returns an effectively unlimited stream of pseudorandom long values from this generator and/or one split from it.

LongStream longs(long streamSize)

Returns a stream producing the given streamSize number of pseudorandom long values from this generator and/or one split from it.

LongStream longs(long randomNumberOrigin, long randomNumberBound)

Returns an effectively unlimited stream of pseudorandom long values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom long values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

boolean nextBoolean()

Returns a pseudorandom boolean value.

double nextDouble()

Returns a pseudorandom double value between zero (inclusive) and one (exclusive).

double nextDouble(double bound)

Returns a pseudorandom double value between 0.0 (inclusive) and the specified bound (exclusive).

double nextDouble(double origin, double bound)

Returns a pseudorandom double value between the specified origin (inclusive) and bound (exclusive).

int nextInt()

Returns a pseudorandom int value.

int nextInt(int origin, int bound)

Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).

int nextInt(int bound)

Returns a pseudorandom int value between zero (inclusive) and the specified bound (exclusive).

long nextLong(long bound)

Returns a pseudorandom long value between zero (inclusive) and the specified bound (exclusive).

long nextLong()

Returns a pseudorandom long value.

long nextLong(long origin, long bound)

Returns a pseudorandom long value between the specified origin (inclusive) and the specified bound (exclusive).

SplittableRandom split()

Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance.

Inherited methods

From class java.lang.Object

Public constructors

SplittableRandom

Added in API level 24
SplittableRandom (long seed)

Creates a new SplittableRandom instance using the specified initial seed. SplittableRandom instances created with the same seed in the same program generate identical sequences of values.

Parameters
seed long: the initial seed

SplittableRandom

Added in API level 24
SplittableRandom ()

Creates a new SplittableRandom instance that is likely to generate sequences of values that are statistically independent of those of any other instances in the current program; and may, and typically does, vary across program invocations.

Public methods

doubles

Added in API level 24
DoubleStream doubles (long streamSize)

Returns a stream producing the given streamSize number of pseudorandom double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).

Parameters
streamSize long: the number of values to generate
Returns
DoubleStream a stream of double values
Throws
IllegalArgumentException if streamSize is less than zero

doubles

Added in API level 24
DoubleStream doubles ()

Returns an effectively unlimited stream of pseudorandom double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).

Implementation Note:
  • This method is implemented to be equivalent to doubles(Long.MAX_VALUE).
Returns
DoubleStream a stream of pseudorandom double values

doubles

Added in API level 24
DoubleStream doubles (double randomNumberOrigin, 
                double randomNumberBound)

Returns an effectively unlimited stream of pseudorandom double values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

Implementation Note:
  • This method is implemented to be equivalent to doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).
Parameters
randomNumberOrigin double: the origin (inclusive) of each random value
randomNumberBound double: the bound (exclusive) of each random value
Returns
DoubleStream a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)
Throws
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

doubles

Added in API level 24
DoubleStream doubles (long streamSize, 
                double randomNumberOrigin, 
                double randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom double values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

Parameters
streamSize long: the number of values to generate
randomNumberOrigin double: the origin (inclusive) of each random value
randomNumberBound double: the bound (exclusive) of each random value
Returns
DoubleStream a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)
Throws
IllegalArgumentException if streamSize is less than zero
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

ints

Added in API level 24
IntStream ints (long streamSize)

Returns a stream producing the given streamSize number of pseudorandom int values from this generator and/or one split from it.

Parameters
streamSize long: the number of values to generate
Returns
IntStream a stream of pseudorandom int values
Throws
IllegalArgumentException if streamSize is less than zero

ints

Added in API level 24
IntStream ints (long streamSize, 
                int randomNumberOrigin, 
                int randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom int values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

Parameters
streamSize long: the number of values to generate
randomNumberOrigin int: the origin (inclusive) of each random value
randomNumberBound int: the bound (exclusive) of each random value
Returns
IntStream a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)
Throws
IllegalArgumentException if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound

ints

Added in API level 24
IntStream ints (int randomNumberOrigin, 
                int randomNumberBound)

Returns an effectively unlimited stream of pseudorandom int values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

Implementation Note:
  • This method is implemented to be equivalent to ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).
Parameters
randomNumberOrigin int: the origin (inclusive) of each random value
randomNumberBound int: the bound (exclusive) of each random value
Returns
IntStream a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)
Throws
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

ints

Added in API level 24
IntStream ints ()

Returns an effectively unlimited stream of pseudorandom int values from this generator and/or one split from it.

Implementation Note:
  • This method is implemented to be equivalent to ints(Long.MAX_VALUE).
Returns
IntStream a stream of pseudorandom int values

longs

Added in API level 24
LongStream longs ()

Returns an effectively unlimited stream of pseudorandom long values from this generator and/or one split from it.

Implementation Note:
  • This method is implemented to be equivalent to longs(Long.MAX_VALUE).
Returns
LongStream a stream of pseudorandom long values

longs

Added in API level 24
LongStream longs (long streamSize)

Returns a stream producing the given streamSize number of pseudorandom long values from this generator and/or one split from it.

Parameters
streamSize long: the number of values to generate
Returns
LongStream a stream of pseudorandom long values
Throws
IllegalArgumentException if streamSize is less than zero

longs

Added in API level 24
LongStream longs (long randomNumberOrigin, 
                long randomNumberBound)

Returns an effectively unlimited stream of pseudorandom long values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

Implementation Note:
  • This method is implemented to be equivalent to longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).
Parameters
randomNumberOrigin long: the origin (inclusive) of each random value
randomNumberBound long: the bound (exclusive) of each random value
Returns
LongStream a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive)
Throws
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

longs

Added in API level 24
LongStream longs (long streamSize, 
                long randomNumberOrigin, 
                long randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom long values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).

Parameters
streamSize long: the number of values to generate
randomNumberOrigin long: the origin (inclusive) of each random value
randomNumberBound long: the bound (exclusive) of each random value
Returns
LongStream a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive)
Throws
IllegalArgumentException if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound

nextBoolean

Added in API level 24
boolean nextBoolean ()

Returns a pseudorandom boolean value.

Returns
boolean a pseudorandom boolean value

nextDouble

Added in API level 24
double nextDouble ()

Returns a pseudorandom double value between zero (inclusive) and one (exclusive).

Returns
double a pseudorandom double value between zero (inclusive) and one (exclusive)

nextDouble

Added in API level 24
double nextDouble (double bound)

Returns a pseudorandom double value between 0.0 (inclusive) and the specified bound (exclusive).

Parameters
bound double: the upper bound (exclusive). Must be positive.
Returns
double a pseudorandom double value between zero (inclusive) and the bound (exclusive)
Throws
IllegalArgumentException if bound is not positive

nextDouble

Added in API level 24
double nextDouble (double origin, 
                double bound)

Returns a pseudorandom double value between the specified origin (inclusive) and bound (exclusive).

Parameters
origin double: the least value returned
bound double: the upper bound (exclusive)
Returns
double a pseudorandom double value between the origin (inclusive) and the bound (exclusive)
Throws
IllegalArgumentException if origin is greater than or equal to bound

nextInt

Added in API level 24
int nextInt ()

Returns a pseudorandom int value.

Returns
int a pseudorandom int value

nextInt

Added in API level 24
int nextInt (int origin, 
                int bound)

Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).

Parameters
origin int: the least value returned
bound int: the upper bound (exclusive)
Returns
int a pseudorandom int value between the origin (inclusive) and the bound (exclusive)
Throws
IllegalArgumentException if origin is greater than or equal to bound

nextInt

Added in API level 24
int nextInt (int bound)

Returns a pseudorandom int value between zero (inclusive) and the specified bound (exclusive).

Parameters
bound int: the upper bound (exclusive). Must be positive.
Returns
int a pseudorandom int value between zero (inclusive) and the bound (exclusive)
Throws
IllegalArgumentException if bound is not positive

nextLong

Added in API level 24
long nextLong (long bound)

Returns a pseudorandom long value between zero (inclusive) and the specified bound (exclusive).

Parameters
bound long: the upper bound (exclusive). Must be positive.
Returns
long a pseudorandom long value between zero (inclusive) and the bound (exclusive)
Throws
IllegalArgumentException if bound is not positive

nextLong

Added in API level 24
long nextLong ()

Returns a pseudorandom long value.

Returns
long a pseudorandom long value

nextLong

Added in API level 24
long nextLong (long origin, 
                long bound)

Returns a pseudorandom long value between the specified origin (inclusive) and the specified bound (exclusive).

Parameters
origin long: the least value returned
bound long: the upper bound (exclusive)
Returns
long a pseudorandom long value between the origin (inclusive) and the bound (exclusive)
Throws
IllegalArgumentException if origin is greater than or equal to bound

split

Added in API level 24
SplittableRandom split ()

Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance. However, with very high probability, the set of values collectively generated by the two objects has the same statistical properties as if the same quantity of values were generated by a single thread using a single SplittableRandom object. Either or both of the two objects may be further split using the split() method, and the same expected statistical properties apply to the entire set of generators constructed by such recursive splitting.

Returns
SplittableRandom the new SplittableRandom instance

Hooray!