MCPcopy
hub / github.com/Effect-TS/effect / RandomImpl

Class RandomImpl

packages/effect/src/internal/random.ts:22–54  ·  view source on GitHub ↗

@internal

Source from the content-addressed store, hash-verified

20export const randomTag: Context.Tag<Random.Random, Random.Random> = Context.GenericTag("effect/Random")
21/** @internal */
22class RandomImpl implements Random.Random {
23 readonly [RandomTypeId]: Random.RandomTypeId = RandomTypeId
24
25 readonly PRNG: PCGRandom.PCGRandom
26
27 constructor(readonly seed: number) {
28 this.PRNG = new PCGRandom.PCGRandom(seed)
29 }
30
31 get next(): Effect.Effect<number> {
32 return core.sync(() => this.PRNG.number())
33 }
34
35 get nextBoolean(): Effect.Effect<boolean> {
36 return core.map(this.next, (n) => n > 0.5)
37 }
38
39 get nextInt(): Effect.Effect<number> {
40 return core.sync(() => this.PRNG.integer(Number.MAX_SAFE_INTEGER))
41 }
42
43 nextRange(min: number, max: number): Effect.Effect<number> {
44 return core.map(this.next, (n) => (max - min) * n + min)
45 }
46
47 nextIntBetween(min: number, max: number): Effect.Effect<number> {
48 return core.sync(() => this.PRNG.integer(max - min) + min)
49 }
50
51 shuffle<A>(elements: Iterable<A>): Effect.Effect<Chunk.Chunk<A>> {
52 return shuffleWith(elements, (n) => this.nextIntBetween(0, n))
53 }
54}
55
56const shuffleWith = <A>(
57 elements: Iterable<A>,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected