(
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.type Effect.Effect = /*unresolved*/ anyEffect<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: Context.Reference<{
nextIntUnsafe(): number
nextDoubleUnsafe(): number
}>
(parameter) 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: Context.Reference<{
nextIntUnsafe(): number
nextDoubleUnsafe(): number
}>
(parameter) r: {
nextIntUnsafe: () => number;
nextDoubleUnsafe: () => number;
}
r.nextDoubleUnsafe() * (const maxInt: numbermaxInt - const minInt: numberminInt + const extra: 0 | 1extra)) + const minInt: numberminInt
})
}