Skip to content

Commit

Permalink
chore: randomizer cleanup (#2431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Oct 6, 2023
1 parent 491d319 commit c0386ac
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/internal/mersenne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class MersenneTwister19937 {

/**
* Generates a MersenneTwister19937 randomizer with 32 bits of precision.
* This is the default randomizer used by Faker.
*
* @internal
*/
Expand Down
2 changes: 1 addition & 1 deletion src/simple-faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class SimpleFaker {
}

/** @internal */
private readonly _randomizer: Randomizer = generateMersenne32Randomizer();
private readonly _randomizer: Randomizer;

readonly datatype: DatatypeModule = new DatatypeModule(this);
readonly date: SimpleDateModule = new SimpleDateModule(this);
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/mersenne.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`mersenne twister > seed: [42,1,2] > should return deterministic value for next() 1`] = `0.8562037434894592`;
exports[`generateMersenne32Randomizer() > seed: [42,1,2] > should return deterministic value for next() 1`] = `0.8562037434894592`;

exports[`mersenne twister > seed: [1211,1,2] > should return deterministic value for next() 1`] = `0.8916433283593506`;
exports[`generateMersenne32Randomizer() > seed: [1211,1,2] > should return deterministic value for next() 1`] = `0.8916433283593506`;

exports[`mersenne twister > seed: [1337,1,2] > should return deterministic value for next() 1`] = `0.17990487208589911`;
exports[`generateMersenne32Randomizer() > seed: [1337,1,2] > should return deterministic value for next() 1`] = `0.17990487208589911`;

exports[`mersenne twister > seed: 42 > should return deterministic value for next() 1`] = `0.37454011430963874`;
exports[`generateMersenne32Randomizer() > seed: 42 > should return deterministic value for next() 1`] = `0.37454011430963874`;

exports[`mersenne twister > seed: 1211 > should return deterministic value for next() 1`] = `0.9285201537422836`;
exports[`generateMersenne32Randomizer() > seed: 1211 > should return deterministic value for next() 1`] = `0.9285201537422836`;

exports[`mersenne twister > seed: 1337 > should return deterministic value for next() 1`] = `0.2620246761944145`;
exports[`generateMersenne32Randomizer() > seed: 1337 > should return deterministic value for next() 1`] = `0.2620246761944145`;
9 changes: 8 additions & 1 deletion test/mersenne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 25;

describe('mersenne twister', () => {
describe('generateMersenne32Randomizer()', () => {
const randomizer: Randomizer = generateMersenne32Randomizer();

it('should return a result matching the interface', () => {
expect(randomizer).toBeDefined();
expect(randomizer).toBeTypeOf('object');
expect(randomizer.next).toBeTypeOf('function');
expect(randomizer.seed).toBeTypeOf('function');
});

describe.each(
[...seededRuns, ...seededRuns.map((v) => [v, 1, 2])].map((v) => [v])
)('seed: %j', (seed) => {
Expand Down

0 comments on commit c0386ac

Please sign in to comment.