Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom randomizer #2284

Merged
merged 33 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
97a7163
feat: custom prng
ST-DDT Jul 31, 2023
b575dc6
Merge branch 'next' into feat/custom-prng
ST-DDT Aug 6, 2023
8a67fc8
chore: rename generateMersennePRNG
ST-DDT Aug 6, 2023
ae647a0
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 15, 2023
1c82b96
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 19, 2023
f3d0165
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 19, 2023
4c3b809
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 19, 2023
646ef43
chore: cleanup
ST-DDT Sep 19, 2023
d2e576d
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 19, 2023
d369826
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 21, 2023
4ca657c
docs: enhance docs
ST-DDT Sep 21, 2023
d40e9e3
chore: rename
ST-DDT Sep 21, 2023
041160c
chore: fix typo
ST-DDT Sep 21, 2023
dad23bc
chore: fix typo
ST-DDT Sep 21, 2023
41e80d4
chore: improve example
ST-DDT Sep 21, 2023
745ffb3
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 22, 2023
34729c8
chore: fix typo
ST-DDT Sep 22, 2023
eea7318
docs: improve jsdoc usage hints
ST-DDT Sep 22, 2023
4487925
chore: move Randomizer to the end
ST-DDT Sep 22, 2023
c6cbcea
chore: simplify
ST-DDT Sep 22, 2023
2374c90
chore: use pure-rand example
ST-DDT Sep 22, 2023
db446d3
docs: create SimpleFaker in example
ST-DDT Sep 22, 2023
e40856d
docs: create SimpleFaker in example
ST-DDT Sep 22, 2023
e3dd059
chore: don't use mersenne
ST-DDT Sep 22, 2023
a072b6e
Update src/randomizer.ts
ST-DDT Sep 22, 2023
42ac780
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 23, 2023
dc78ddf
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 24, 2023
cb3691f
docs: simplify
ST-DDT Sep 24, 2023
7a39e1a
Merge branch 'next' into feat/custom-prng
ST-DDT Sep 29, 2023
6bf7d56
docs: improve documentation
ST-DDT Oct 2, 2023
3574711
Merge branch 'next' into feat/custom-prng
ST-DDT Oct 2, 2023
67bada3
docs: slightly adjust examples
ST-DDT Oct 2, 2023
f98c2ee
chore: add nav entry
ST-DDT Oct 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vitepress/api-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export const apiPages = [
{ text: 'System', link: '/api/system.html' },
{ text: 'Vehicle', link: '/api/vehicle.html' },
{ text: 'Word', link: '/api/word.html' },
{ text: 'Randomizer', link: '/api/randomizer.html' },
{ text: 'Utilities', link: '/api/utils.html' },
];
46 changes: 28 additions & 18 deletions scripts/apidoc/fakerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,33 @@ export async function processFakerClasses(
return Promise.all(fakerClasses.map(processClass));
}

export async function processFakerRandomizer(
project: ProjectReflection
): Promise<ModuleSummary> {
const randomizerClass = project
.getChildrenByKind(ReflectionKind.Interface)
.find((clazz) => clazz.name === 'Randomizer');

return processClass(randomizerClass);
}

async function processClass(
fakerClass: DeclarationReflection
clazz: DeclarationReflection
): Promise<ModuleSummary> {
const { name } = fakerClass;
const moduleFieldName = extractModuleFieldName(fakerClass);
const { name } = clazz;
const moduleFieldName = extractModuleFieldName(clazz);

console.log(`Processing ${name} class`);

const { comment, deprecated, examples } = analyzeModule(fakerClass);
const { comment, deprecated, examples } = analyzeModule(clazz);
const methods: Method[] = [];

console.debug(`- constructor`);
methods.push(await processConstructor(fakerClass));
if (hasConstructor(clazz)) {
console.debug(`- constructor`);
methods.push(await processConstructor(clazz));
}

methods.push(
...(await processModuleMethods(fakerClass, `${moduleFieldName}.`))
);
methods.push(...(await processModuleMethods(clazz, `${moduleFieldName}.`)));

return writeApiDocsModule(
name,
Expand All @@ -49,20 +59,20 @@ async function processClass(
);
}

function hasConstructor(clazz: DeclarationReflection): boolean {
return clazz
.getChildrenByKind(ReflectionKind.Constructor)
.some((constructor) => constructor.signatures.length > 0);
}

async function processConstructor(
fakerClass: DeclarationReflection
clazz: DeclarationReflection
): Promise<Method> {
const constructor = fakerClass.getChildrenByKind(
ReflectionKind.Constructor
)[0];
const constructor = clazz.getChildrenByKind(ReflectionKind.Constructor)[0];

const signature = selectApiSignature(constructor);

const method = await analyzeSignature(
signature,
'',
`new ${fakerClass.name}`
);
const method = await analyzeSignature(signature, '', `new ${clazz.name}`);

return {
...method,
Expand Down
3 changes: 2 additions & 1 deletion scripts/apidoc/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
writeApiSearchIndex,
writeSourceBaseUrl,
} from './apiDocsWriter';
import { processFakerClasses } from './fakerClass';
import { processFakerClasses, processFakerRandomizer } from './fakerClass';
import { processFakerUtilities } from './fakerUtilities';
import { processModules } from './moduleMethods';
import { loadProject } from './typedoc';
Expand All @@ -27,6 +27,7 @@ export async function generate(): Promise<void> {
...(await processModules(project)).sort((a, b) =>
a.text.localeCompare(b.text)
),
await processFakerRandomizer(project),
processFakerUtilities(project),
]);
await writeApiPagesIndex(pages.map(({ text, link }) => ({ text, link })));
Expand Down
35 changes: 33 additions & 2 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ScienceModule } from './modules/science';
import { SystemModule } from './modules/system';
import { VehicleModule } from './modules/vehicle';
import { WordModule } from './modules/word';
import type { Randomizer } from './randomizer';
import { SimpleFaker } from './simple-faker';
import { mergeLocales } from './utils/merge-locales';

Expand Down Expand Up @@ -123,6 +124,10 @@ export class Faker extends SimpleFaker {
*
* @param options The options to use.
* @param options.locale The locale data to use.
* @param options.randomizer The Randomizer to use.
* Specify this only if you want to use it to achieve a specific goal,
* such as sharing the same random generator with other instances/tools.
* Defaults to faker's Mersenne Twister based pseudo random number generator.
*
* @example
* import { Faker, es } from '@faker-js/faker';
Expand All @@ -144,6 +149,15 @@ export class Faker extends SimpleFaker {
* @see mergeLocales
*/
locale: LocaleDefinition | LocaleDefinition[];

/**
* The Randomizer to use.
* Specify this only if you want to use it to achieve a specific goal,
* such as sharing the same random generator with other instances/tools.
*
* @default generateMersenne32Randomizer()
*/
randomizer?: Randomizer;
});
/**
* Creates a new instance of Faker.
Expand Down Expand Up @@ -180,6 +194,10 @@ export class Faker extends SimpleFaker {
* @param options.locale The locale data to use or the name of the main locale.
* @param options.locales The locale data to use.
* @param options.localeFallback The name of the fallback locale to use.
* @param options.randomizer The Randomizer to use.
* Specify this only if you want to use it to achieve a specific goal,
* such as sharing the same random generator with other instances/tools.
* Defaults to faker's Mersenne Twister based pseudo random number generator.
*
* @example
* import { Faker, es } from '@faker-js/faker';
Expand All @@ -203,6 +221,15 @@ export class Faker extends SimpleFaker {
* @see mergeLocales
*/
locale: LocaleDefinition | LocaleDefinition[];

/**
* The Randomizer to use.
* Specify this only if you want to use it to achieve a specific goal,
* such as sharing the same random generator with other instances/tools.
*
* @default generateMersenne32Randomizer()
*/
randomizer?: Randomizer;
}
| {
/**
Expand Down Expand Up @@ -231,14 +258,18 @@ export class Faker extends SimpleFaker {
);
constructor(
options:
| { locale: LocaleDefinition | LocaleDefinition[] }
| {
locale: LocaleDefinition | LocaleDefinition[];
randomizer?: Randomizer;
}
| {
locales: Record<string, LocaleDefinition>;
locale?: string;
localeFallback?: string;
randomizer?: Randomizer;
}
) {
super();
super({ randomizer: options.randomizer });

const { locales } = options as {
locales: Record<string, LocaleDefinition>;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@ export type { StringModule } from './modules/string';
export type { SystemModule } from './modules/system';
export type { VehicleModule } from './modules/vehicle';
export type { WordModule } from './modules/word';
export type { Randomizer } from './randomizer';
export { SimpleFaker, simpleFaker } from './simple-faker';
export { mergeLocales } from './utils/merge-locales';
28 changes: 27 additions & 1 deletion src/internal/mersenne/twister.ts → src/internal/mersenne.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Randomizer } from '../randomizer';

/**
* Copyright (c) 2022-2023 Faker
*
Expand Down Expand Up @@ -71,7 +73,7 @@
*
* @internal
*/
export default class MersenneTwister19937 {
class MersenneTwister19937 {
private readonly N = 624;
private readonly M = 397;
private readonly MATRIX_A = 0x9908b0df; // constant vector a
Expand Down Expand Up @@ -323,3 +325,27 @@ export default class MersenneTwister19937 {
}
// These real versions are due to Isaku Wada, 2002/01/09
}

/**
* Generates a MersenneTwister19937 randomizer with 32 bits of precision.
*
* @internal
*/
export function generateMersenne32Randomizer(): Randomizer {
const twister = new MersenneTwister19937();

twister.initGenrand(Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER));

return {
next(): number {
return twister.genrandReal2();
},
seed(seed: number | number[]): void {
if (typeof seed === 'number') {
twister.initGenrand(seed);
} else if (Array.isArray(seed)) {
twister.initByArray(seed, seed.length);
}
},
};
}
45 changes: 0 additions & 45 deletions src/internal/mersenne/mersenne.ts

This file was deleted.

12 changes: 5 additions & 7 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { SimpleFaker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import type { Mersenne } from '../../internal/mersenne/mersenne';

/**
* Module to generate numbers of any kind.
Expand Down Expand Up @@ -83,10 +82,9 @@ export class NumberModule {
throw new FakerError(`Max ${max} should be greater than min ${min}.`);
}

const mersenne: Mersenne =
// @ts-expect-error: access private member field
this.faker._mersenne;
const real = mersenne.next();
// @ts-expect-error: access private member field
const randomizer = this.faker._randomizer;
const real = randomizer.next();
return Math.floor(real * (effectiveMax + 1 - effectiveMin) + effectiveMin);
}

Expand Down Expand Up @@ -160,8 +158,8 @@ export class NumberModule {
}

// @ts-expect-error: access private member field
const mersenne: Mersenne = this.faker._mersenne;
const real = mersenne.next();
const randomizer = this.faker._randomizer;
const real = randomizer.next();
return real * (max - min) + min;
}

Expand Down
57 changes: 57 additions & 0 deletions src/randomizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
* Interface for a random number generator.
*
* **Note:** Normally there is no need to implement this interface directly,
* unless you want to achieve a specific goal with it.
*
* This interface enables you to use random generators from third party libraries such as [pure-rand](https://github.com/dubzzz/pure-rand).
*
* Instances are expected to be ready for use before being passed to any Faker constructor,
* this includes being `seed()`ed with either a random or fixed value.
*
* @example
* import { Randomizer, SimpleFaker } from '@faker-js/faker';
* import { RandomGenerator, xoroshiro128plus } from 'pure-rand';
*
* function generatePureRandRandomizer(
* seed: number | number[] = Date.now() ^ (Math.random() * 0x100000000),
* factory: (seed: number) => RandomGenerator = xoroshiro128plus
* ): Randomizer {
* const self = {
* next: () => (self.generator.unsafeNext() >>> 0) / 0x100000000,
* seed: (seed: number | number[]) => {
* self.generator = factory(typeof seed === 'number' ? seed : seed[0]);
* },
* } as Randomizer & { generator: RandomGenerator };
* self.seed(seed);
* return self;
* }
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*
* const simpleFaker = new SimpleFaker({ randomizer: generatePureRandRandomizer() });
*/
export interface Randomizer {
/**
* Generates a random float between `[0, 1)`.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
* This method is called `next` so that it could be used as an [iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol).
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* randomizer.next() // 0.3404027920160495
* randomizer.next() // 0.929890375900335
* randomizer.next() // 0.5866362918861691
*/
next(): number;

/**
* Sets the seed to use.
*
* @param seed The seed to use.
*
* @example
* // Random seeds
* randomizer.seed(Date.now() ^ (Math.random() * 0x100000000));
* // Fixed seeds (for reproducibility)
* randomizer.seed(42);
* randomizer.seed([42, 13.37]);
*/
seed(seed: number | number[]): void;
}

Check warning on line 57 in src/randomizer.ts

View check run for this annotation

Codecov / codecov/patch

src/randomizer.ts#L2-L57

Added lines #L2 - L57 were not covered by tests