Skip to content

Commit

Permalink
fix(snippet): limit seed to 32bits according to splitmix32
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed May 24, 2024
1 parent f47279c commit 649edc4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if (query.has('seed')) {
query
.get('seed')
.replace(/[^0-9a-f]/gi, 'f')
.padEnd(12, 'f'),
.padEnd(8, 'f'),
16
) % Number.MAX_SAFE_INTEGER;
query.set('seed', $o.seed.toString(16));
Expand Down
10 changes: 10 additions & 0 deletions test/test.snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ describe('$o', () => {
assert.equal($o.rnd(), 0.03844817215576768);
});

it("splitmix32's seed is in [0, 2^32-1] and wraps", () => {
const { $o } = init();
$o.seed = 0;
$o.rnd(null);
assert.equal($o.rnd(), 0.8505931859835982);
$o.seed = 2 ** 32;
$o.rnd(null);
assert.equal($o.rnd(), 0.8505931859835982);
});

it('can be reset by passing null', () => {
const { $o } = init();
$o.seed = 928173;
Expand Down

0 comments on commit 649edc4

Please sign in to comment.