TestRandom

zio.test.TestRandom
See theTestRandom companion object
trait TestRandom extends Random, Restorable

TestRandom allows for deterministically testing effects involving randomness.

TestRandom operates in two modes. In the first mode, TestRandom is a purely functional pseudo-random number generator. It will generate pseudo-random values just like scala.util.Random except that no internal state is mutated. Instead, methods like nextInt describe state transitions from one random state to another that are automatically composed together through methods like flatMap. The random seed can be set using setSeed and TestRandom is guaranteed to return the same sequence of values for any given seed. This is useful for deterministically generating a sequence of pseudo-random values and powers the property based testing functionality in ZIO Test.

In the second mode, TestRandom maintains an internal buffer of values that can be "fed" with methods such as feedInts and then when random values of that type are generated they will first be taken from the buffer. This is useful for verifying that functions produce the expected output for a given sequence of "random" inputs.

import zio.Random
import zio.test.TestRandom

for {
 _ <- TestRandom.feedInts(4, 5, 2)
 x <- Random.nextIntBounded(6)
 y <- Random.nextIntBounded(6)
 z <- Random.nextIntBounded(6)
} yield x + y + z == 11

TestRandom will automatically take values from the buffer if a value of the appropriate type is available and otherwise generate a pseudo-random value, so there is nothing you need to do to switch between the two modes. Just generate random values as you normally would to get pseudo-random values, or feed in values of your own to get those values back. You can also use methods like clearInts to clear the buffer of values of a given type so you can fill the buffer with new values or go back to pseudo-random number generation.

Attributes

Companion
object
Source
TestRandom.scala
Graph
Supertypes
trait Restorable
trait Random
trait Serializable
class Object
trait Matchable
class Any
Show all
Known subtypes
class Test

Members list

Type members

Inherited classlikes

trait UnsafeAPI extends Serializable

Attributes

Inherited from:
Random
Source
Random.scala
Supertypes
trait Serializable
class Object
trait Matchable
class Any

Value members

Abstract methods

def clearBooleans(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def clearBytes(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def clearChars(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def clearDoubles(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def clearFloats(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def clearInts(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def clearLongs(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def clearStrings(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def clearUUIDs(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedBooleans(booleans: Boolean*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedBytes(bytes: Chunk[Byte]*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedChars(chars: Char*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedDoubles(doubles: Double*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedFloats(floats: Float*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedInts(ints: Int*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedLongs(longs: Long*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedStrings(strings: String*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def feedUUIDs(UUIDs: UUID*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala
def getSeed(implicit trace: Trace): UIO[Long]

Attributes

Source
TestRandom.scala
def setSeed(seed: => Long)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestRandom.scala

Inherited methods

Attributes

Inherited from:
Random
Source
Random.scala

Inherited and Abstract methods

def nextBoolean(implicit trace: Trace): UIO[Boolean]

Attributes

Inherited from:
Random
Source
Random.scala
def nextBytes(length: => Int)(implicit trace: Trace): UIO[Chunk[Byte]]

Attributes

Inherited from:
Random
Source
Random.scala
def nextDouble(implicit trace: Trace): UIO[Double]

Attributes

Inherited from:
Random
Source
Random.scala
def nextDoubleBetween(minInclusive: => Double, maxExclusive: => Double)(implicit trace: Trace): UIO[Double]

Attributes

Inherited from:
Random
Source
Random.scala
def nextFloat(implicit trace: Trace): UIO[Float]

Attributes

Inherited from:
Random
Source
Random.scala
def nextFloatBetween(minInclusive: => Float, maxExclusive: => Float)(implicit trace: Trace): UIO[Float]

Attributes

Inherited from:
Random
Source
Random.scala
def nextGaussian(implicit trace: Trace): UIO[Double]

Attributes

Inherited from:
Random
Source
Random.scala
def nextInt(implicit trace: Trace): UIO[Int]

Attributes

Inherited from:
Random
Source
Random.scala
def nextIntBetween(minInclusive: => Int, maxExclusive: => Int)(implicit trace: Trace): UIO[Int]

Attributes

Inherited from:
Random
Source
Random.scala
def nextIntBounded(n: => Int)(implicit trace: Trace): UIO[Int]

Attributes

Inherited from:
Random
Source
Random.scala
def nextLong(implicit trace: Trace): UIO[Long]

Attributes

Inherited from:
Random
Source
Random.scala
def nextLongBetween(minInclusive: => Long, maxExclusive: => Long)(implicit trace: Trace): UIO[Long]

Attributes

Inherited from:
Random
Source
Random.scala
def nextLongBounded(n: => Long)(implicit trace: Trace): UIO[Long]

Attributes

Inherited from:
Random
Source
Random.scala
def nextPrintableChar(implicit trace: Trace): UIO[Char]

Attributes

Inherited from:
Random
Source
Random.scala
def nextString(length: => Int)(implicit trace: Trace): UIO[String]

Attributes

Inherited from:
Random
Source
Random.scala
def nextUUID(implicit trace: Trace): UIO[UUID]

Attributes

Inherited from:
Random
Source
Random.scala
def save(implicit trace: Trace): UIO[UIO[Unit]]

Attributes

Inherited from:
Restorable
Source
Restorable.scala
def shuffle[A, Collection <: (Iterable)](collection: => Collection[A])(implicit bf: BuildFrom[Collection[A], A, Collection[A]], trace: Trace): UIO[Collection[A]]

Attributes

Inherited from:
Random
Source
Random.scala