(
min: number,
max: number,
options?: { readonly halfOpen?: boolean }
): Effect.Effect<number>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)
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)
})export const const nextIntBetween: (
min: number,
max: number,
options?: { readonly halfOpen?: boolean }
) => Effect.Effect<number>
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)
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)
})
nextIntBetween = (min: numbermin: number, max: numbermax: number, options: | {
readonly halfOpen?: boolean
}
| undefined
options?: {
readonly halfOpen?: boolean | undefinedhalfOpen?: boolean
}): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<number> => {
const const extra: 0 | 1extra = options: | {
readonly halfOpen?: boolean
}
| undefined
options?.halfOpen?: boolean | undefinedhalfOpen === true ? 0 : 1
return const randomWith: <A>(
f: (random: (typeof Random)["Service"]) => A
) => Effect.Effect<A>
randomWith((r: {
nextIntUnsafe(): number
nextDoubleUnsafe(): number
}
r) => {
const const minInt: numberminInt = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.ceil(x: number): numberReturns the smallest integer greater than or equal to its numeric argument.
ceil(min: numbermin)
const const maxInt: numbermaxInt = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.floor(x: number): numberReturns the greatest integer less than or equal to its numeric argument.
floor(max: numbermax)
return var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.floor(x: number): numberReturns the greatest integer less than or equal to its numeric argument.
floor(r: {
nextIntUnsafe(): number
nextDoubleUnsafe(): number
}
r.function nextDoubleUnsafe(): numbernextDoubleUnsafe() * (const maxInt: numbermaxInt - const minInt: numberminInt + const extra: 0 | 1extra)) + const minInt: numberminInt
})
}