Skip to content

Commit

Permalink
export Random.make taking hashable values as seed (#3203)
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomoran authored and gcanti committed Jul 10, 2024
1 parent a46fdb8 commit 10b2f49
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-insects-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

export Random.make taking hashable values as seed
9 changes: 9 additions & 0 deletions packages/effect/src/Random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,12 @@ export const randomWith: <A, E, R>(f: (random: Random) => Effect.Effect<A, E, R>
* @category context
*/
export const Random: Context.Tag<Random, Random> = internal.randomTag

/**
* Constructs the `Random` service, seeding the pseudo-random number generator
* with an hash of the specified seed.
*
* @since 3.5.0
* @category constructors
*/
export const make: <A>(seed: A) => Random = internal.make
2 changes: 1 addition & 1 deletion packages/effect/src/internal/defaultServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const liveServices: Context.Context<DefaultServices.DefaultServices> = pi
Context.empty(),
Context.add(clock.clockTag, clock.make()),
Context.add(console_.consoleTag, console_.defaultConsole),
Context.add(random.randomTag, random.make((Math.random() * 4294967296) >>> 0)),
Context.add(random.randomTag, random.make(Math.random())),
Context.add(configProvider.configProviderTag, configProvider.fromEnv()),
Context.add(tracer.tracerTag, tracer.nativeTracer)
)
Expand Down
3 changes: 2 additions & 1 deletion packages/effect/src/internal/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Chunk from "../Chunk.js"
import * as Context from "../Context.js"
import type * as Effect from "../Effect.js"
import { pipe } from "../Function.js"
import * as Hash from "../Hash.js"
import type * as Random from "../Random.js"
import * as PCGRandom from "../Utils.js"
import * as core from "./core.js"
Expand Down Expand Up @@ -85,4 +86,4 @@ const swap = <A>(buffer: Array<A>, index1: number, index2: number): Array<A> =>
return buffer
}

export const make = (seed: number): Random.Random => new RandomImpl(seed)
export const make = <A>(seed: A): Random.Random => new RandomImpl(Hash.hash(seed))
17 changes: 16 additions & 1 deletion packages/effect/test/Random.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Array, Chunk, Effect, Random } from "effect"
import { Array, Chunk, Data, Effect, Random } from "effect"
import * as it from "effect/test/utils/extend"
import { assert, describe } from "vitest"

Expand All @@ -10,4 +10,19 @@ describe("Random", () => {
assert.isTrue(Chunk.every(end, (n) => n !== undefined))
assert.deepStrictEqual(start.sort(), Array.fromIterable(end).sort())
}).pipe(Effect.repeatN(100)))

it.effect("make", () =>
Effect.gen(function*() {
const random0 = Random.make("foo")
const random1 = Random.make("foo")
const random2 = Random.make(Data.struct({ foo: "bar" }))
const random3 = Random.make(Data.struct({ foo: "bar" }))
const n0 = yield* random0.next
const n1 = yield* random1.next
const n2 = yield* random2.next
const n3 = yield* random3.next
assert.strictEqual(n0, n1)
assert.strictEqual(n2, n3)
assert.notStrictEqual(n0, n2)
}))
})

0 comments on commit 10b2f49

Please sign in to comment.