Random¶
Functions¶
accessUnsafe¶
suspend fun accessUnsafe(): Random
Gives uncontrolled access to the underlying Random.
Warning. The Kotlin standard library's random generator is not thread-safe. This class wraps it with the necessary synchronization mechanisms. By using accessUnsafe, you are bypassing them. Incorrect usage of this function may break the random generation and reproducibility guarantees of this class.
In most cases, use is probably sufficient.
nextBits¶
Generates random bits.
See also
-
Random.nextBits: Standard library. -
randomBits: Prepared value equivalent.
nextBoolean¶
suspend fun Random.nextBoolean(): Boolean
Generates a random boolean.
See also
-
Random.nextBoolean: Standard library. -
randomBoolean: Prepared value equivalent.
nextDouble¶
suspend fun Random.nextDouble(): Double
Generates a random double.
See also
-
Random.nextDouble: Standard library. -
randomDouble: Prepared value equivalent.
suspend fun Random.nextDouble(from: Double, until: Double): Double
Generates a random double.
See also
-
Random.nextDouble: Standard library. -
randomDouble: Prepared value equivalent.
nextFloat¶
Generates a random float.
See also
-
Random.nextFloat: Standard library. -
randomFloat: Prepared value equivalent.
nextInt¶
Generates a random integer.
See also
-
Random.nextInt: Standard library. -
randomInt: Prepared value equivalent.
Generates a random integer.
See also
-
Random.nextInt: Standard library. -
randomInt: Prepared value equivalent.
nextLong¶
Generates a random integer.
See also
-
Random.nextLong: Standard library. -
randomLong: Prepared value equivalent.
Generates a random integer.
See also
-
Random.nextLong: Standard library. -
randomLong: Prepared value equivalent.
setSeed¶
Initializes the underlying Random implementation with seed.
Example:
val int by randomInt()
test("This is a test") {
setSeed(123456789L)
// Even if prepared values are declared outside the test,
// as long as they are accessed after the seed is set,
// they respect the configured seed.
println("Generated number: ${int()}")
}
This function is meant to easily allow reproducing a test failure that only arrives in rare cases by simply adding it at the start of the test with the seed of the failed execution.
This function can only be called before the first random value is generated for the current test, otherwise it throws IllegalStateException.