/** * Provides pseudo-random generation through an Effect service. * * This module exposes effectful generators for booleans, doubles, safe * integers, bounded numbers, shuffling, and deterministic seeded runs. Because * random generation is a service, tests and applications can replace the * generator used by Effect programs. * * @stability stable * @since 4.0.0 */ import type * as Arr from "./Array.ts"; import * as Cause from "./Cause.ts"; import type * as Context from "./Context.ts"; import * as Effect from "./Effect.ts"; import type * as NonEmptyIterable from "./NonEmptyIterable.ts"; /** * The service used to generate pseudo-random numbers. * * @stability stable * @category services * @since 4.0.0 */ export interface Random { /** * Generates a random safe integer. */ nextIntUnsafe(): number; /** * Generates a random number between 0 (inclusive) and 1 (exclusive). */ nextDoubleUnsafe(): number; } /** * Represents a service for generating pseudo-random numbers. * * **When to use** * * Use to access or provide the random-number generator service used by Effect * programs. * * **Gotchas** * * The default implementation is based on `Math.random` and is not * cryptographically secure. Replace the service with a cryptographically secure * implementation before using these generators for security-sensitive values. * * **Example** (Accessing the random service) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * const program = Effect.gen(function*() { * const float = yield* Random.next * const integer = yield* Random.nextInt * const inRange = yield* Random.nextIntBetween(1, 100) * return [float, integer, inRange] as const * }) * * await Effect.runPromise(program.pipe(Random.withSeed("example"))) // => [0.1633802591287037, 3434461687501127, 1] * ``` * * @stability stable * @category services * @since 2.0.0 */ export declare const Random: Context.Reference; /** * Generates a random number between 0 (inclusive) and 1 (exclusive). * * **When to use** * * Use to generate a pseudo-random floating-point number in the standard * `[0, 1)` range. * * **Example** (Generating a random number) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * await Effect.runPromise(Random.next.pipe(Random.withSeed("example"))) // => 0.1633802591287037 * ``` * * @stability stable * @category generators * @since 2.0.0 */ export declare const next: Effect.Effect; /** * Generates a random boolean value. * * **When to use** * * Use to make a pseudo-random true-or-false choice. * * **Example** (Generating a random boolean) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * await Effect.runPromise(Random.nextBoolean.pipe(Random.withSeed("example"))) // => false * ``` * * @stability stable * @category generators * @since 2.0.0 */ export declare const nextBoolean: Effect.Effect; /** * Generates a random integer between `Number.MIN_SAFE_INTEGER` (inclusive) * and `Number.MAX_SAFE_INTEGER` (inclusive). * * **When to use** * * Use to generate a pseudo-random safe integer across the full safe-integer * range. * * **Example** (Generating a random integer) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * await Effect.runPromise(Random.nextInt.pipe(Random.withSeed("example"))) // => -6064002158214091 * ``` * * @stability stable * @category generators * @since 2.0.0 */ export declare const nextInt: Effect.Effect; /** * Generates a random number between `min` (inclusive) and `max` (exclusive). * * **When to use** * * Use to generate a pseudo-random floating-point number within a numeric range. * * **Example** (Generating a bounded random number) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * await Effect.runPromise(Random.nextBetween(0, 1).pipe(Random.withSeed("example"))) // => 0.1633802591287037 * ``` * * @stability stable * @category generators * @since 4.0.0 */ export declare const nextBetween: (min: number, max: number) => Effect.Effect; /** * Generates a random integer between `min` and `max`. * * **When to use** * * Use to generate a pseudo-random integer within a rounded numeric range. * * **Details** * * The lower bound is rounded up with `Math.ceil` and the upper bound is * rounded down with `Math.floor`. By default the range is inclusive; set * `options.halfOpen: true` to exclude the upper bound. * * **Example** (Generating a bounded random integer) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * const program = Effect.gen(function*() { * const diceRoll1 = yield* Random.nextIntBetween(1, 6) * const diceRoll2 = yield* Random.nextIntBetween(1, 6, { * halfOpen: true * }) * const diceRoll3 = yield* Random.nextIntBetween(0, 10) * return [diceRoll1, diceRoll2, diceRoll3] * }) * * await Effect.runPromise(program.pipe(Random.withSeed("example"))) // => [1, 4, 0] * ``` * * @stability stable * @category generators * @since 2.0.0 */ export declare const nextIntBetween: (min: number, max: number, options?: { readonly halfOpen?: boolean; }) => Effect.Effect; /** * Uses the pseudo-random number generator to shuffle the specified iterable. * * **When to use** * * Use to randomly reorder an iterable using the active `Random` service. * * **Example** (Shuffling values) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * await Effect.runPromise(Random.shuffle([1, 2, 3, 4, 5]).pipe(Random.withSeed("example"))) // => [4, 2, 5, 3, 1] * ``` * * @stability stable * @category generators * @since 2.0.0 */ export declare const shuffle: (elements: Iterable) => Effect.Effect>; /** * Gets a random element from an iterable. * * **When to use** * * Use to select one value uniformly from a collection using the active `Random` * service. * * **Details** * * If the input type is known to be non-empty, the returned effect cannot fail. * Otherwise, empty iterables fail with `Cause.NoSuchElementError`. * * **Example** (Choosing a random value) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * await Effect.runPromise(Random.choice(["red", "green", "blue"] as const).pipe(Random.withSeed("example"))) // => "red" * ``` * * @stability stable * @category generators * @since 3.6.0 */ export declare const choice: >(elements: Self) => Self extends NonEmptyIterable.NonEmptyIterable ? Effect.Effect : Self extends Arr.NonEmptyReadonlyArray ? Effect.Effect : Self extends Iterable ? Effect.Effect : never; /** * 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) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * const program = Effect.gen(function*() { * const value1 = yield* Random.next * const value2 = yield* Random.next * return [value1, value2] * }) * * await Effect.runPromise(Effect.all([ * program.pipe(Random.withSeed("my-seed")), * program.pipe(Random.withSeed("my-seed")) * ])) // => [[0.018368576514773527, 0.4010840628128671], [0.018368576514773527, 0.4010840628128671]] * ``` * * @stability stable * @category providing services * @since 4.0.0 */ export declare const withSeed: { /** * 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) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * const program = Effect.gen(function*() { * const value1 = yield* Random.next * const value2 = yield* Random.next * return [value1, value2] * }) * * await Effect.runPromise(Effect.all([ * program.pipe(Random.withSeed("my-seed")), * program.pipe(Random.withSeed("my-seed")) * ])) // => [[0.018368576514773527, 0.4010840628128671], [0.018368576514773527, 0.4010840628128671]] * ``` * * @stability stable * @category providing services * @since 4.0.0 */ (seed: string | number): (self: Effect.Effect) => Effect.Effect; /** * 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) * * ```ts import.meta.vitest * import { Effect, Random } from "effect" * * const program = Effect.gen(function*() { * const value1 = yield* Random.next * const value2 = yield* Random.next * return [value1, value2] * }) * * await Effect.runPromise(Effect.all([ * program.pipe(Random.withSeed("my-seed")), * program.pipe(Random.withSeed("my-seed")) * ])) // => [[0.018368576514773527, 0.4010840628128671], [0.018368576514773527, 0.4010840628128671]] * ``` * * @stability stable * @category providing services * @since 4.0.0 */ (self: Effect.Effect, seed: string | number): Effect.Effect; }; //# sourceMappingURL=Random.d.ts.map