@fgv/ts-random 5.1.0-8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/jest.config.json +13 -0
- package/config/rig.json +4 -0
- package/config/typedoc.json +7 -0
- package/docs/Chars/README.md +152 -0
- package/docs/Chars/variables/all.md +9 -0
- package/docs/Chars/variables/alphanumeric.md +9 -0
- package/docs/Chars/variables/base64.md +9 -0
- package/docs/Chars/variables/base64Url.md +9 -0
- package/docs/Chars/variables/base64UrlNoPadding.md +9 -0
- package/docs/Chars/variables/digits.md +9 -0
- package/docs/Chars/variables/hex.md +9 -0
- package/docs/Chars/variables/hexDigits.md +9 -0
- package/docs/Chars/variables/hexDigitsUpper.md +9 -0
- package/docs/Chars/variables/letters.md +9 -0
- package/docs/Chars/variables/lowerCase.md +9 -0
- package/docs/Chars/variables/printable.md +9 -0
- package/docs/Chars/variables/symbols.md +9 -0
- package/docs/Chars/variables/upperCase.md +9 -0
- package/docs/Chars/variables/whitespace.md +9 -0
- package/docs/Generator/README.md +153 -0
- package/docs/Generator/classes/PseudoRandomGenerator.clone.md +17 -0
- package/docs/Generator/classes/PseudoRandomGenerator.create.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.createChild.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.ensureRng.md +25 -0
- package/docs/Generator/classes/PseudoRandomGenerator.getGlobalRng.md +17 -0
- package/docs/Generator/classes/PseudoRandomGenerator.md +258 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextBoolean.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextFloat.md +17 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextInRange.md +25 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextInt.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextString.md +25 -0
- package/docs/Generator/classes/PseudoRandomGenerator.pickNext.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.pickRandom.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.pickSequence.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.pickSequential.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.rng.md +11 -0
- package/docs/Generator/classes/PseudoRandomGenerator.setGlobalRng.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.clone.md +17 -0
- package/docs/Generator/classes/SeededRandomSource.counter.md +11 -0
- package/docs/Generator/classes/SeededRandomSource.create.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.createChild.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.hashSeed.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.hashStateAndLabel.md +25 -0
- package/docs/Generator/classes/SeededRandomSource.lineage.md +9 -0
- package/docs/Generator/classes/SeededRandomSource.md +187 -0
- package/docs/Generator/classes/SeededRandomSource.mulberryStep.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.next.md +17 -0
- package/docs/Generator/classes/SeededRandomSource.seed.md +9 -0
- package/docs/Generator/interfaces/INextResult.md +61 -0
- package/docs/Generator/interfaces/INextResult.nextState.md +9 -0
- package/docs/Generator/interfaces/INextResult.value.md +9 -0
- package/docs/Generator/interfaces/IPseudoRandomGeneratorCreateParams.global.md +9 -0
- package/docs/Generator/interfaces/IPseudoRandomGeneratorCreateParams.md +78 -0
- package/docs/Generator/interfaces/IPseudoRandomGeneratorCreateParams.seed.md +9 -0
- package/docs/Generator/interfaces/IPseudoRandomGeneratorCreateParams.step.md +9 -0
- package/docs/Generator/interfaces/IRandomSequencePickParams.candidates.md +9 -0
- package/docs/Generator/interfaces/IRandomSequencePickParams.count.md +9 -0
- package/docs/Generator/interfaces/IRandomSequencePickParams.how.md +9 -0
- package/docs/Generator/interfaces/IRandomSequencePickParams.md +79 -0
- package/docs/Generator/interfaces/ISeedPair.md +61 -0
- package/docs/Generator/interfaces/ISeedPair.seed.md +9 -0
- package/docs/Generator/interfaces/ISeedPair.state.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.counter.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.lineage.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.md +112 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.seed.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.state.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.step.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceCreateParams.md +61 -0
- package/docs/Generator/interfaces/ISeededRandomSourceCreateParams.seed.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceCreateParams.step.md +9 -0
- package/docs/Generator/interfaces/ISequentialPickParams.candidates.md +9 -0
- package/docs/Generator/interfaces/ISequentialPickParams.count.md +9 -0
- package/docs/Generator/interfaces/ISequentialPickParams.how.md +9 -0
- package/docs/Generator/interfaces/ISequentialPickParams.md +79 -0
- package/docs/Generator/type-aliases/GeneratorGlobalThis.md +11 -0
- package/docs/Generator/type-aliases/RandomStepFunction.md +11 -0
- package/docs/Generator/type-aliases/SequencePickParams.md +11 -0
- package/docs/GeneratorData/Chars/README.md +152 -0
- package/docs/GeneratorData/Chars/variables/all.md +9 -0
- package/docs/GeneratorData/Chars/variables/alphanumeric.md +9 -0
- package/docs/GeneratorData/Chars/variables/base64.md +9 -0
- package/docs/GeneratorData/Chars/variables/base64Url.md +9 -0
- package/docs/GeneratorData/Chars/variables/base64UrlNoPadding.md +9 -0
- package/docs/GeneratorData/Chars/variables/digits.md +9 -0
- package/docs/GeneratorData/Chars/variables/hex.md +9 -0
- package/docs/GeneratorData/Chars/variables/hexDigits.md +9 -0
- package/docs/GeneratorData/Chars/variables/hexDigitsUpper.md +9 -0
- package/docs/GeneratorData/Chars/variables/letters.md +9 -0
- package/docs/GeneratorData/Chars/variables/lowerCase.md +9 -0
- package/docs/GeneratorData/Chars/variables/printable.md +9 -0
- package/docs/GeneratorData/Chars/variables/symbols.md +9 -0
- package/docs/GeneratorData/Chars/variables/upperCase.md +9 -0
- package/docs/GeneratorData/Chars/variables/whitespace.md +9 -0
- package/docs/GeneratorData/README.md +35 -0
- package/docs/GeneratorData/Words/README.md +125 -0
- package/docs/GeneratorData/Words/variables/Adjectives.md +7 -0
- package/docs/GeneratorData/Words/variables/Animals.md +7 -0
- package/docs/GeneratorData/Words/variables/Cities.md +7 -0
- package/docs/GeneratorData/Words/variables/Colors.md +7 -0
- package/docs/GeneratorData/Words/variables/Countries.md +7 -0
- package/docs/GeneratorData/Words/variables/Domains.md +7 -0
- package/docs/GeneratorData/Words/variables/FamilyNames.md +9 -0
- package/docs/GeneratorData/Words/variables/FirstNames.md +9 -0
- package/docs/GeneratorData/Words/variables/FirstNamesFemale.md +9 -0
- package/docs/GeneratorData/Words/variables/FirstNamesMale.md +9 -0
- package/docs/GeneratorData/Words/variables/Gerunds.md +7 -0
- package/docs/GeneratorData/Words/variables/jobs.md +7 -0
- package/docs/GeneratorData/variables/Adjectives.md +7 -0
- package/docs/GeneratorData/variables/Animals.md +7 -0
- package/docs/GeneratorData/variables/Cities.md +7 -0
- package/docs/GeneratorData/variables/Colors.md +7 -0
- package/docs/GeneratorData/variables/Countries.md +7 -0
- package/docs/GeneratorData/variables/Domains.md +7 -0
- package/docs/GeneratorData/variables/FamilyNames.md +9 -0
- package/docs/GeneratorData/variables/FirstNames.md +9 -0
- package/docs/GeneratorData/variables/FirstNamesFemale.md +9 -0
- package/docs/GeneratorData/variables/FirstNamesMale.md +9 -0
- package/docs/GeneratorData/variables/Gerunds.md +7 -0
- package/docs/GeneratorData/variables/all.md +9 -0
- package/docs/GeneratorData/variables/alphanumeric.md +9 -0
- package/docs/GeneratorData/variables/base64.md +9 -0
- package/docs/GeneratorData/variables/base64Url.md +9 -0
- package/docs/GeneratorData/variables/base64UrlNoPadding.md +9 -0
- package/docs/GeneratorData/variables/digits.md +9 -0
- package/docs/GeneratorData/variables/hex.md +9 -0
- package/docs/GeneratorData/variables/hexDigits.md +9 -0
- package/docs/GeneratorData/variables/hexDigitsUpper.md +9 -0
- package/docs/GeneratorData/variables/jobs.md +7 -0
- package/docs/GeneratorData/variables/letters.md +9 -0
- package/docs/GeneratorData/variables/lowerCase.md +9 -0
- package/docs/GeneratorData/variables/printable.md +9 -0
- package/docs/GeneratorData/variables/symbols.md +9 -0
- package/docs/GeneratorData/variables/upperCase.md +9 -0
- package/docs/GeneratorData/variables/whitespace.md +9 -0
- package/docs/README.md +35 -0
- package/docs/Words/README.md +125 -0
- package/docs/Words/variables/Adjectives.md +7 -0
- package/docs/Words/variables/Animals.md +7 -0
- package/docs/Words/variables/Cities.md +7 -0
- package/docs/Words/variables/Colors.md +7 -0
- package/docs/Words/variables/Countries.md +7 -0
- package/docs/Words/variables/Domains.md +7 -0
- package/docs/Words/variables/FamilyNames.md +9 -0
- package/docs/Words/variables/FirstNames.md +9 -0
- package/docs/Words/variables/FirstNamesFemale.md +9 -0
- package/docs/Words/variables/FirstNamesMale.md +9 -0
- package/docs/Words/variables/Gerunds.md +7 -0
- package/docs/Words/variables/jobs.md +7 -0
- package/docs/classes/PseudoRandomGenerator.clone.md +17 -0
- package/docs/classes/PseudoRandomGenerator.create.md +24 -0
- package/docs/classes/PseudoRandomGenerator.createChild.md +24 -0
- package/docs/classes/PseudoRandomGenerator.ensureRng.md +25 -0
- package/docs/classes/PseudoRandomGenerator.getGlobalRng.md +17 -0
- package/docs/classes/PseudoRandomGenerator.md +258 -0
- package/docs/classes/PseudoRandomGenerator.nextBoolean.md +24 -0
- package/docs/classes/PseudoRandomGenerator.nextFloat.md +17 -0
- package/docs/classes/PseudoRandomGenerator.nextInRange.md +25 -0
- package/docs/classes/PseudoRandomGenerator.nextInt.md +24 -0
- package/docs/classes/PseudoRandomGenerator.nextString.md +25 -0
- package/docs/classes/PseudoRandomGenerator.pickNext.md +24 -0
- package/docs/classes/PseudoRandomGenerator.pickRandom.md +24 -0
- package/docs/classes/PseudoRandomGenerator.pickSequence.md +24 -0
- package/docs/classes/PseudoRandomGenerator.pickSequential.md +24 -0
- package/docs/classes/PseudoRandomGenerator.rng.md +11 -0
- package/docs/classes/PseudoRandomGenerator.setGlobalRng.md +24 -0
- package/docs/classes/SeededRandomSource.clone.md +17 -0
- package/docs/classes/SeededRandomSource.counter.md +11 -0
- package/docs/classes/SeededRandomSource.create.md +24 -0
- package/docs/classes/SeededRandomSource.createChild.md +24 -0
- package/docs/classes/SeededRandomSource.hashSeed.md +24 -0
- package/docs/classes/SeededRandomSource.hashStateAndLabel.md +25 -0
- package/docs/classes/SeededRandomSource.lineage.md +9 -0
- package/docs/classes/SeededRandomSource.md +187 -0
- package/docs/classes/SeededRandomSource.mulberryStep.md +24 -0
- package/docs/classes/SeededRandomSource.next.md +17 -0
- package/docs/classes/SeededRandomSource.seed.md +9 -0
- package/docs/interfaces/INextResult.md +61 -0
- package/docs/interfaces/INextResult.nextState.md +9 -0
- package/docs/interfaces/INextResult.value.md +9 -0
- package/docs/interfaces/IPseudoRandomGeneratorCreateParams.global.md +9 -0
- package/docs/interfaces/IPseudoRandomGeneratorCreateParams.md +78 -0
- package/docs/interfaces/IPseudoRandomGeneratorCreateParams.seed.md +9 -0
- package/docs/interfaces/IPseudoRandomGeneratorCreateParams.step.md +9 -0
- package/docs/interfaces/IRandomSequencePickParams.candidates.md +9 -0
- package/docs/interfaces/IRandomSequencePickParams.count.md +9 -0
- package/docs/interfaces/IRandomSequencePickParams.how.md +9 -0
- package/docs/interfaces/IRandomSequencePickParams.md +79 -0
- package/docs/interfaces/ISeedPair.md +61 -0
- package/docs/interfaces/ISeedPair.seed.md +9 -0
- package/docs/interfaces/ISeedPair.state.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.counter.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.lineage.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.md +112 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.seed.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.state.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.step.md +9 -0
- package/docs/interfaces/ISeededRandomSourceCreateParams.md +61 -0
- package/docs/interfaces/ISeededRandomSourceCreateParams.seed.md +9 -0
- package/docs/interfaces/ISeededRandomSourceCreateParams.step.md +9 -0
- package/docs/interfaces/ISequentialPickParams.candidates.md +9 -0
- package/docs/interfaces/ISequentialPickParams.count.md +9 -0
- package/docs/interfaces/ISequentialPickParams.how.md +9 -0
- package/docs/interfaces/ISequentialPickParams.md +79 -0
- package/docs/type-aliases/GeneratorGlobalThis.md +11 -0
- package/docs/type-aliases/RandomStepFunction.md +11 -0
- package/docs/type-aliases/SequencePickParams.md +11 -0
- package/docs/variables/Adjectives.md +7 -0
- package/docs/variables/Animals.md +7 -0
- package/docs/variables/Cities.md +7 -0
- package/docs/variables/Colors.md +7 -0
- package/docs/variables/Countries.md +7 -0
- package/docs/variables/Domains.md +7 -0
- package/docs/variables/FamilyNames.md +9 -0
- package/docs/variables/FirstNames.md +9 -0
- package/docs/variables/FirstNamesFemale.md +9 -0
- package/docs/variables/FirstNamesMale.md +9 -0
- package/docs/variables/Gerunds.md +7 -0
- package/docs/variables/all.md +9 -0
- package/docs/variables/alphanumeric.md +9 -0
- package/docs/variables/base64.md +9 -0
- package/docs/variables/base64Url.md +9 -0
- package/docs/variables/base64UrlNoPadding.md +9 -0
- package/docs/variables/digits.md +9 -0
- package/docs/variables/hex.md +9 -0
- package/docs/variables/hexDigits.md +9 -0
- package/docs/variables/hexDigitsUpper.md +9 -0
- package/docs/variables/jobs.md +7 -0
- package/docs/variables/letters.md +9 -0
- package/docs/variables/lowerCase.md +9 -0
- package/docs/variables/printable.md +9 -0
- package/docs/variables/symbols.md +9 -0
- package/docs/variables/upperCase.md +9 -0
- package/docs/variables/whitespace.md +9 -0
- package/package.json +49 -0
- package/src/generator/generators.ts +273 -0
- package/src/generator/index.ts +22 -0
- package/src/generator/randomSource.ts +224 -0
- package/src/generator-data/charClasses.ts +95 -0
- package/src/generator-data/index.ts +22 -0
- package/src/generator-data/words/adjectives.ts +247 -0
- package/src/generator-data/words/animals.ts +75 -0
- package/src/generator-data/words/cities.ts +229 -0
- package/src/generator-data/words/colors.ts +56 -0
- package/src/generator-data/words/countries.ts +217 -0
- package/src/generator-data/words/domains.ts +47 -0
- package/src/generator-data/words/gerunds.ts +126 -0
- package/src/generator-data/words/index.ts +29 -0
- package/src/generator-data/words/jobs.ts +175 -0
- package/src/generator-data/words/names.ts +717 -0
- package/src/index.ts +7 -0
- package/src/test/unit/generators.test.ts +697 -0
- package/src/test/unit/randomSource.test.ts +526 -0
- package/tsconfig.json +7 -0
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fgv/ts-random",
|
|
3
|
+
"version": "5.1.0-8",
|
|
4
|
+
"description": "psedo-random number generator and utilities for TypeScript",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "heft build --clean",
|
|
9
|
+
"clean": "heft clean",
|
|
10
|
+
"test": "heft test --clean",
|
|
11
|
+
"coverage": "heft test --coverage --no-cache",
|
|
12
|
+
"build-docs": "typedoc --options ./config/typedoc.json",
|
|
13
|
+
"lint": "eslint src --ext .ts",
|
|
14
|
+
"fixlint": "eslint src --ext .ts --fix"
|
|
15
|
+
},
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@fgv/ts-utils": "workspace:*",
|
|
20
|
+
"@fgv/ts-json-base": "workspace:*"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@fgv/heft-dual-rig": "workspace:*",
|
|
24
|
+
"@rushstack/heft": "1.2.7",
|
|
25
|
+
"@rushstack/heft-node-rig": "2.11.27",
|
|
26
|
+
"@fgv/ts-utils-jest": "workspace:*",
|
|
27
|
+
"@types/heft-jest": "1.0.6",
|
|
28
|
+
"@types/jest": "^29.5.14",
|
|
29
|
+
"@types/node": "^20.14.9",
|
|
30
|
+
"typescript": "5.9.3",
|
|
31
|
+
"@rushstack/eslint-config": "4.6.4",
|
|
32
|
+
"eslint": "^9.39.2",
|
|
33
|
+
"typedoc": "~0.28.16",
|
|
34
|
+
"@fgv/typedoc-compact-theme": "workspace:*"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": ""
|
|
39
|
+
},
|
|
40
|
+
"module": "dist/index.js",
|
|
41
|
+
"exports": {
|
|
42
|
+
".": {
|
|
43
|
+
"types": "./lib/index.d.ts",
|
|
44
|
+
"import": "./dist/index.js",
|
|
45
|
+
"require": "./lib/index.js",
|
|
46
|
+
"default": "./lib/index.js"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// Copyright (c) 2026 Erik Fortune
|
|
2
|
+
//
|
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
// in the Software without restriction, including without limitation the rights
|
|
6
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
// furnished to do so, subject to the following conditions:
|
|
9
|
+
//
|
|
10
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
// copies or substantial portions of the Software.
|
|
12
|
+
//
|
|
13
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
// SOFTWARE.
|
|
20
|
+
|
|
21
|
+
import { captureResult, Result, Success } from '@fgv/ts-utils';
|
|
22
|
+
import { RandomStepFunction, SeededRandomSource } from './randomSource';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Parameters for creating a generator.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export interface IPseudoRandomGeneratorCreateParams {
|
|
29
|
+
seed?: number | string;
|
|
30
|
+
step?: RandomStepFunction;
|
|
31
|
+
global?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Parameters for generating a sequence of values
|
|
36
|
+
* from each dictionary in a supplied set, in turn.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export interface ISequentialPickParams<T> {
|
|
40
|
+
how: 'sequence';
|
|
41
|
+
count?: number;
|
|
42
|
+
candidates: ReadonlyArray<ReadonlyArray<T>>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parameters for generating a sequence of values
|
|
47
|
+
* by randomly selecting from dictionaries in a supplied set.
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
export interface IRandomSequencePickParams<T> {
|
|
51
|
+
how: 'random';
|
|
52
|
+
count: number;
|
|
53
|
+
candidates: ReadonlyArray<ReadonlyArray<T>>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Type representing parameters for generating a sequence of values.
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
export type SequencePickParams<T> = ISequentialPickParams<T> | IRandomSequencePickParams<T>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A pseudo-random number generator that can be used to generate random values
|
|
64
|
+
* of various shapes.
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
export class PseudoRandomGenerator {
|
|
68
|
+
private static _globalRng: PseudoRandomGenerator | undefined;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The underlying random number generator.
|
|
72
|
+
*/
|
|
73
|
+
public readonly rng: SeededRandomSource;
|
|
74
|
+
|
|
75
|
+
private constructor(rng: SeededRandomSource) {
|
|
76
|
+
this.rng = rng;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new pseudo-random number generator.
|
|
81
|
+
* @param params - The parameters for creating the generator.
|
|
82
|
+
* @returns A result containing the new generator or an error.
|
|
83
|
+
*/
|
|
84
|
+
public static create(params: IPseudoRandomGeneratorCreateParams = {}): Result<PseudoRandomGenerator> {
|
|
85
|
+
return SeededRandomSource.create(params)
|
|
86
|
+
.onSuccess((rng) => captureResult(() => new PseudoRandomGenerator(rng)))
|
|
87
|
+
.onSuccess((generator) => {
|
|
88
|
+
if (params?.global === true) {
|
|
89
|
+
PseudoRandomGenerator.setGlobalRng(generator);
|
|
90
|
+
}
|
|
91
|
+
return Success.with(generator);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Creates a clone of this generator.
|
|
97
|
+
* @returns A new generator with the same state.
|
|
98
|
+
*/
|
|
99
|
+
public clone(): PseudoRandomGenerator {
|
|
100
|
+
return new PseudoRandomGenerator(this.rng.clone());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Creates a child generator with the given label.
|
|
105
|
+
* @param label - The label for the child generator.
|
|
106
|
+
* @returns A new generator with the given label.
|
|
107
|
+
*/
|
|
108
|
+
public createChild(label: string): PseudoRandomGenerator {
|
|
109
|
+
return new PseudoRandomGenerator(this.rng.createChild(label));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Generates a random float between 0 and 1.
|
|
114
|
+
* @returns A random float between 0 and 1.
|
|
115
|
+
*/
|
|
116
|
+
public nextFloat(): number {
|
|
117
|
+
return this.rng.next();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Generates a random integer between 0 and the specified extent (positive or negative).
|
|
122
|
+
* @param extent - The maximum (or minimum) value (exclusive).
|
|
123
|
+
* @returns A random integer between 0 and the specified extent.
|
|
124
|
+
*/
|
|
125
|
+
public nextInt(extent?: number): number {
|
|
126
|
+
if (extent !== undefined && Number.isFinite(extent)) {
|
|
127
|
+
if (extent < 0) {
|
|
128
|
+
return Math.ceil(this.rng.next() * extent);
|
|
129
|
+
}
|
|
130
|
+
return Math.floor(this.rng.next() * extent);
|
|
131
|
+
}
|
|
132
|
+
return Math.floor(this.rng.next() * Number.MAX_SAFE_INTEGER);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Generates a random integer between the specified minimum and maximum values.
|
|
137
|
+
* @param min - The minimum value (inclusive).
|
|
138
|
+
* @param max - The maximum value (inclusive).
|
|
139
|
+
* @returns A random integer between the specified minimum and maximum values.
|
|
140
|
+
* @remarks If min is greater than max, the values are swapped.
|
|
141
|
+
*/
|
|
142
|
+
public nextInRange(min: number | undefined, max: number | undefined): number {
|
|
143
|
+
min = min ?? 0;
|
|
144
|
+
max = max ?? Number.MAX_SAFE_INTEGER;
|
|
145
|
+
if (min > max) {
|
|
146
|
+
return Math.ceil(this.rng.next() * (min - max - 1)) + max;
|
|
147
|
+
}
|
|
148
|
+
return Math.floor(this.rng.next() * (max - min + 1)) + min;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Generates a random boolean value.
|
|
153
|
+
* @param trueProbability - The probability of returning true (default is 0.5).
|
|
154
|
+
* @returns A random boolean value.
|
|
155
|
+
*/
|
|
156
|
+
public nextBoolean(trueProbability: number = 0.5): boolean {
|
|
157
|
+
return this.rng.next() < trueProbability;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Generates a random string of the specified length using the given characters.
|
|
162
|
+
* @param length - The length of the string to generate.
|
|
163
|
+
* @param chars - The characters to use for the string (default is alphanumeric).
|
|
164
|
+
* @returns A random string of the specified length.
|
|
165
|
+
*/
|
|
166
|
+
public nextString(
|
|
167
|
+
length: number,
|
|
168
|
+
chars: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
169
|
+
): string {
|
|
170
|
+
let result = '';
|
|
171
|
+
for (let i = 0; i < length; i++) {
|
|
172
|
+
result += chars[this.nextInt(chars.length)];
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Generates a random item from the given array.
|
|
179
|
+
* @param items - The array to select from.
|
|
180
|
+
* @returns A random item from the array or undefined if the array is empty.
|
|
181
|
+
*/
|
|
182
|
+
public pickNext<T>(items?: ReadonlyArray<T>): T | undefined {
|
|
183
|
+
if (items === undefined || items.length === 0) {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
return items[this.nextInt(items.length)];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Generates a sequence of values by randomly selecting from the given candidate
|
|
191
|
+
* dictionaries in order.
|
|
192
|
+
* @param params - The parameters for generating the sequence.
|
|
193
|
+
* @returns A sequence of values.
|
|
194
|
+
*/
|
|
195
|
+
public pickSequential<T>(params: ISequentialPickParams<T>): ReadonlyArray<T> {
|
|
196
|
+
const count = params.count ?? params.candidates.length;
|
|
197
|
+
const items: T[] = [];
|
|
198
|
+
|
|
199
|
+
for (let i = 0; i < count; i++) {
|
|
200
|
+
const dIndex = i % params.candidates.length;
|
|
201
|
+
const item = this.pickNext(params.candidates[dIndex]);
|
|
202
|
+
if (item !== undefined) {
|
|
203
|
+
items.push(item);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return items;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Generates a sequence of values by randomly selecting from the supplied
|
|
212
|
+
* dictionaries in random order.
|
|
213
|
+
* @param params - The parameters for generating the sequence.
|
|
214
|
+
* @returns A sequence of values.
|
|
215
|
+
*/
|
|
216
|
+
public pickRandom<T>(params: IRandomSequencePickParams<T>): ReadonlyArray<T> {
|
|
217
|
+
const items: T[] = [];
|
|
218
|
+
|
|
219
|
+
for (let i = 0; i < params.count; i++) {
|
|
220
|
+
const dIndex = this.nextInt(params.candidates.length);
|
|
221
|
+
const item = this.pickNext(params.candidates[dIndex]);
|
|
222
|
+
if (item !== undefined) {
|
|
223
|
+
items.push(item);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return items;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Generates a sequence of values by randomly selecting from the given candidates.
|
|
232
|
+
* @param params - The parameters for generating the sequence.
|
|
233
|
+
* @returns A sequence of values.
|
|
234
|
+
*/
|
|
235
|
+
public pickSequence<T>(params: SequencePickParams<T>): ReadonlyArray<T> {
|
|
236
|
+
if (params.how === 'sequence') {
|
|
237
|
+
return this.pickSequential(params);
|
|
238
|
+
} else {
|
|
239
|
+
return this.pickRandom(params);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Sets this generator as the global random number generator.
|
|
245
|
+
* @returns A result containing this generator or an error.
|
|
246
|
+
*/
|
|
247
|
+
public static setGlobalRng(rng?: PseudoRandomGenerator): PseudoRandomGenerator | undefined {
|
|
248
|
+
PseudoRandomGenerator._globalRng = rng;
|
|
249
|
+
return rng;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Gets the global random number generator.
|
|
254
|
+
* @returns The global random number generator or undefined if not set.
|
|
255
|
+
*/
|
|
256
|
+
public static getGlobalRng(): PseudoRandomGenerator | undefined {
|
|
257
|
+
return PseudoRandomGenerator._globalRng;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Ensures a random number generator is available, using the global generator if available
|
|
262
|
+
* or creating a new one if not.
|
|
263
|
+
* @param rng - The random number generator to use, or undefined to use the global one.
|
|
264
|
+
* @returns A random number generator.
|
|
265
|
+
*/
|
|
266
|
+
public static ensureRng(rng?: PseudoRandomGenerator): Result<PseudoRandomGenerator> {
|
|
267
|
+
rng = rng ?? PseudoRandomGenerator.getGlobalRng();
|
|
268
|
+
if (rng) {
|
|
269
|
+
return Success.with(rng);
|
|
270
|
+
}
|
|
271
|
+
return PseudoRandomGenerator.create();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright (c) 2026 Erik Fortune
|
|
2
|
+
//
|
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
// in the Software without restriction, including without limitation the rights
|
|
6
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
// furnished to do so, subject to the following conditions:
|
|
9
|
+
//
|
|
10
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
// copies or substantial portions of the Software.
|
|
12
|
+
//
|
|
13
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
// SOFTWARE.
|
|
20
|
+
|
|
21
|
+
export * from './randomSource';
|
|
22
|
+
export * from './generators';
|