Skip to content

Commit

Permalink
✨ Expose internal state of the PRNG from Random (#4817)
Browse files Browse the repository at this point in the history
In the context of #4063, we need to have an access onto the internal state of the PRNG backing the instance of `Random` in order to be able to serialize it and recreate the "exact" same instance of `Random` on destination (another worker thread).

The current PR adds a `getState` method on `Random`.

Adding it should be enough to fulfill the needs of #4063. But we will wait for it to be confirmed before releasing any minor version including that change.
  • Loading branch information
dubzzz authored Mar 20, 2024
1 parent fc9f689 commit 74a9d3a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .yarn/versions/a6ab7de6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
releases:
fast-check: minor

declined:
- "@fast-check/ava"
- "@fast-check/jest"
- "@fast-check/vitest"
- "@fast-check/worker"
2 changes: 1 addition & 1 deletion packages/fast-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"node": ">=8.0.0"
},
"dependencies": {
"pure-rand": "^6.0.0"
"pure-rand": "^6.1.0"
},
"devDependencies": {
"@babel/core": "^7.24.3",
Expand Down
10 changes: 10 additions & 0 deletions packages/fast-check/src/random/generator/Random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,14 @@ export class Random {
const b = this.next(27);
return (a * Random.DBL_FACTOR + b) * Random.DBL_DIVISOR;
}

/**
* Extract the internal state of the internal RandomGenerator backing the current instance of Random
*/
getState(): readonly number[] | undefined {
if ('getState' in this.internalRng && typeof this.internalRng.getState === 'function') {
return this.internalRng.getState();
}
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ export function fakeRandom(): { instance: Random } & Omit<MaybeMocked<Random>, '
const nextBigInt = jest.fn();
const nextArrayInt = jest.fn();
const nextDouble = jest.fn();
const getState = jest.fn();
class MyRandom extends Random {
clone = clone;
next = next;
nextBoolean = nextBoolean;
nextInt = nextInt;
nextBigInt = nextBigInt;
nextArrayInt = nextArrayInt;
nextDouble = nextDouble;
getState = getState;
}

// Calling `new MyRandom` triggers a call to the default ctor of `Random`.
// As we don't use anything from this base class, we just pass the ctor with a value that looks ok for it.
const instance = new MyRandom({ clone: () => null } as any);
return { instance, clone, next, nextBoolean, nextInt, nextBigInt, nextArrayInt, nextDouble };
return { instance, clone, next, nextBoolean, nextInt, nextBigInt, nextArrayInt, nextDouble, getState };
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9466,7 +9466,7 @@ __metadata:
glob: "npm:^10.3.10"
jest: "npm:^29.7.0"
not-node-buffer: "npm:buffer@^6.0.3"
pure-rand: "npm:^6.0.0"
pure-rand: "npm:^6.1.0"
regexp-tree: "npm:^0.1.27"
replace-in-file: "npm:^7.1.0"
typedoc: "npm:^0.25.12"
Expand Down Expand Up @@ -15980,10 +15980,10 @@ __metadata:
languageName: node
linkType: hard

"pure-rand@npm:^6.0.0, pure-rand@npm:^6.0.4":
version: 6.0.4
resolution: "pure-rand@npm:6.0.4"
checksum: 10c0/0fe7b12f25b10ea5b804598a6f37e4bcf645d2be6d44fe963741f014bf0095bdb6ff525106d6da6e76addc8142358fd380f1a9b8c62ea4d5516bf26a96a37c95
"pure-rand@npm:^6.0.0, pure-rand@npm:^6.0.4, pure-rand@npm:^6.1.0":
version: 6.1.0
resolution: "pure-rand@npm:6.1.0"
checksum: 10c0/1abe217897bf74dcb3a0c9aba3555fe975023147b48db540aa2faf507aee91c03bf54f6aef0eb2bf59cc259a16d06b28eca37f0dc426d94f4692aeff02fb0e65
languageName: node
linkType: hard

Expand Down

0 comments on commit 74a9d3a

Please sign in to comment.