Google is committed to advancing racial equity for Black communities. See how.
Added in API level 24

SplittableRandom

class SplittableRandom
kotlin.Any
   ↳ 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 java.util.Random but differs in the following ways:

  • Series of generated values pass the DieHarder suite testing independence and uniformity properties of random number generators. (Most recently validated with version 3.31.1.) These tests validate only the methods for certain types and ranges, but similar properties are expected to hold, at least approximately, for others as well. The period (length of any series of generated values before it repeats) is at least 264.
  • Method split constructs and returns a new SplittableRandom instance that shares no mutable state with the current instance. However, with very high probability, the values collectively generated by the two objects have the same statistical properties as if the same quantity of values were generated by a single thread using a single SplittableRandom object.
  • Instances of SplittableRandom are not thread-safe. They are designed to be split, not shared, across threads. For example, a fork/join-style computation using random numbers might include a construction of the form new Subtask(aSplittableRandom.split()).fork().
  • This class provides additional methods for generating random streams, that employ the above techniques when used in stream.parallel() mode.

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

Summary

Public constructors
<init>(seed: Long)

Creates a new SplittableRandom instance using the specified initial seed.

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(streamSize: Long)

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!

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(streamSize: Long, randomNumberOrigin: Double, randomNumberBound: Double)

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).

DoubleStream!
doubles(randomNumberOrigin: Double, randomNumberBound: Double)

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).

IntStream!
ints(streamSize: Long)

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

IntStream!

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

IntStream!
ints(streamSize: Long, randomNumberOrigin: Int, randomNumberBound: Int)

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(randomNumberOrigin: Int, randomNumberBound: Int)

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).

LongStream!
longs(streamSize: Long)

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

LongStream!

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

LongStream!
longs(streamSize: Long, randomNumberOrigin: Long, randomNumberBound: Long)

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).

LongStream!
longs(randomNumberOrigin: Long, randomNumberBound: Long)

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).

Boolean

Returns a pseudorandom boolean value.

Double

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

Double

Returns a pseudorandom double value between 0.

Double
nextDouble(origin: Double, bound: Double)

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

Int

Returns a pseudorandom int value.

Int
nextInt(bound: Int)

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

Int
nextInt(origin: Int, bound: Int)

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

Long

Returns a pseudorandom long value.

Long
nextLong(bound: Long)

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

Long
nextLong(origin: Long, bound: Long)

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

SplittableRandom!

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

Public constructors

<init>

Added in API level 24
SplittableRandom(seed: Long)

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

<init>

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
fun doubles(streamSize: Long): DoubleStream!

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
Return
DoubleStream! a stream of double values
Exceptions
java.lang.IllegalArgumentException if streamSize is less than zero

doubles

Added in API level 24
fun doubles(): DoubleStream!

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).

Return
DoubleStream! a stream of pseudorandom double values

doubles

Added in API level 24
fun doubles(
    streamSize: Long,
    randomNumberOrigin: Double,
    randomNumberBound: Double
): DoubleStream!

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
Return
DoubleStream! a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

doubles

Added in API level 24
fun doubles(
    randomNumberOrigin: Double,
    randomNumberBound: Double
): DoubleStream!

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).

Parameters
randomNumberOrigin Double: the origin (inclusive) of each random value
randomNumberBound Double: the bound (exclusive) of each random value
Return
DoubleStream! a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

ints

Added in API level 24
fun ints(streamSize: Long): IntStream!

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
Return
IntStream! a stream of pseudorandom int values
Exceptions
java.lang.IllegalArgumentException if streamSize is less than zero

ints

Added in API level 24
fun ints(): IntStream!

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

Return
IntStream! a stream of pseudorandom int values

ints

Added in API level 24
fun ints(
    streamSize: Long,
    randomNumberOrigin: Int,
    randomNumberBound: Int
): IntStream!

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
Return
IntStream! a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound

ints

Added in API level 24
fun ints(
    randomNumberOrigin: Int,
    randomNumberBound: Int
): IntStream!

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).

Parameters
randomNumberOrigin Int: the origin (inclusive) of each random value
randomNumberBound Int: the bound (exclusive) of each random value
Return
IntStream! a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

longs

Added in API level 24
fun longs(streamSize: Long): LongStream!

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
Return
LongStream! a stream of pseudorandom long values
Exceptions
java.lang.IllegalArgumentException if streamSize is less than zero

longs

Added in API level 24
fun longs(): LongStream!

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

Return
LongStream! a stream of pseudorandom long values

longs

Added in API level 24
fun longs(
    streamSize: Long,
    randomNumberOrigin: Long,
    randomNumberBound: Long
): LongStream!

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
Return
LongStream! a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound

longs

Added in API level 24
fun longs(
    randomNumberOrigin: Long,
    randomNumberBound: Long
): LongStream!

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).

Parameters
randomNumberOrigin Long: the origin (inclusive) of each random value
randomNumberBound Long: the bound (exclusive) of each random value
Return
LongStream! a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

nextBoolean

Added in API level 24
fun nextBoolean(): Boolean

Returns a pseudorandom boolean value.

Return
Boolean a pseudorandom boolean value

nextDouble

Added in API level 24
fun nextDouble(): Double

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

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

nextDouble

Added in API level 24
fun nextDouble(bound: Double): Double

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

Parameters
bound Double: the upper bound (exclusive). Must be positive.
Return
Double a pseudorandom double value between zero (inclusive) and the bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if bound is not positive

nextDouble

Added in API level 24
fun nextDouble(
    origin: Double,
    bound: Double
): Double

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)
Return
Double a pseudorandom double value between the origin (inclusive) and the bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if origin is greater than or equal to bound

nextInt

Added in API level 24
fun nextInt(): Int

Returns a pseudorandom int value.

Return
Int a pseudorandom int value

nextInt

Added in API level 24
fun nextInt(bound: Int): Int

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

Parameters
bound Int: the upper bound (exclusive). Must be positive.
Return
Int a pseudorandom int value between zero (inclusive) and the bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if bound is not positive

nextInt

Added in API level 24
fun nextInt(
    origin: Int,
    bound: Int
): Int

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)
Return
Int a pseudorandom int value between the origin (inclusive) and the bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if origin is greater than or equal to bound

nextLong

Added in API level 24
fun nextLong(): Long

Returns a pseudorandom long value.

Return
Long a pseudorandom long value

nextLong

Added in API level 24
fun nextLong(bound: Long): Long

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

Parameters
bound Long: the upper bound (exclusive). Must be positive.
Return
Long a pseudorandom long value between zero (inclusive) and the bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if bound is not positive

nextLong

Added in API level 24
fun nextLong(
    origin: Long,
    bound: Long
): Long

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)
Return
Long a pseudorandom long value between the origin (inclusive) and the bound (exclusive)
Exceptions
java.lang.IllegalArgumentException if origin is greater than or equal to bound

split

Added in API level 24
fun split(): SplittableRandom!

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.

Return
SplittableRandom! the new SplittableRandom instance