Hyperlinkv0.8.0-beta.28

Random

Random.withSeedconsteffect/Random.ts:309
(seed: string | number): <A, E, R>(
  self: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
<A, E, R>(
  self: Effect.Effect<A, E, R>,
  seed: string | number
): Effect.Effect<A, E, R>

Seeds the pseudo-random number generator with the specified value.

When to use

Use to run an effect with a deterministic pseudo-random sequence.

Details

Using the same seed produces the same random sequence, which is useful for tests and reproducible simulations.

Gotchas

Use an unpredictable seed when uniqueness or unpredictability matters.

Example (Seeding random generation)

import { Effect, Random } from "effect"

const program = Effect.gen(function*() {
  const value1 = yield* Random.next
  const value2 = yield* Random.next
  console.log(value1, value2)
})

// Same seed produces same sequence
const seeded1 = program.pipe(Random.withSeed("my-seed"))
const seeded2 = program.pipe(Random.withSeed("my-seed"))

// Both will output identical values
Effect.runPromise(seeded1)
Effect.runPromise(seeded2)
Seeding
Source effect/Random.ts:3097 lines
export const withSeed: {
  (seed: string | number): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
  <A, E, R>(self: Effect.Effect<A, E, R>, seed: string | number): Effect.Effect<A, E, R>
} = dual(2, <A, E, R>(
  self: Effect.Effect<A, E, R>,
  seed: string | number
) => Effect.provideService(self, Random, ISAAC_CSPRNG(seed)))