toosoon-prng 2.1.0 → 2.4.0
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/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/{src/index.ts → lib/index.d.ts} +0 -1
- package/lib/index.js +1 -0
- package/lib/prng.d.ts +106 -0
- package/lib/prng.js +175 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types.d.ts +7 -0
- package/lib/types.js +8 -0
- package/lib/utils.d.ts +45 -0
- package/lib/utils.js +113 -0
- package/package.json +7 -3
- package/tsconfig.json +1 -3
- package/src/prng.ts +0 -180
- package/src/types.ts +0 -7
- package/src/utils.ts +0 -119
package/.prettierrc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 TOOSOON
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default, PRNG } from './prng';
|
package/lib/prng.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { AlgorithmName } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Utility class for generating pseudo-random values
|
|
4
|
+
*
|
|
5
|
+
* @class PRNG
|
|
6
|
+
* @exports
|
|
7
|
+
*/
|
|
8
|
+
export declare class PRNG {
|
|
9
|
+
seed: string;
|
|
10
|
+
algorithm: (...args: number[]) => number;
|
|
11
|
+
/**
|
|
12
|
+
* Set this PRNG seed
|
|
13
|
+
*
|
|
14
|
+
* @param {string} seed
|
|
15
|
+
*/
|
|
16
|
+
setSeed(seed: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* Set this PRNG algorithm
|
|
19
|
+
*
|
|
20
|
+
* @param {AlgorithmName} algorithmName Algorithm name
|
|
21
|
+
*/
|
|
22
|
+
setAlgorithm(algorithmName: AlgorithmName): void;
|
|
23
|
+
/**
|
|
24
|
+
* Generate a pseudo-random number in the interval [0, 1]
|
|
25
|
+
* PRNG equivalent of `Math.random()`
|
|
26
|
+
*
|
|
27
|
+
* @param {string} seed
|
|
28
|
+
* @returns {number}
|
|
29
|
+
*/
|
|
30
|
+
random(seed: string): number;
|
|
31
|
+
/**
|
|
32
|
+
* Generate a pseudo-random boolean (true or false)
|
|
33
|
+
*
|
|
34
|
+
* @param {string} seed
|
|
35
|
+
* @param {number} [probability=0.5] Probability to get `true`
|
|
36
|
+
* @returns {boolean} Either `true` or `false`
|
|
37
|
+
*/
|
|
38
|
+
randomBoolean(seed: string, probability?: number): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Generate a pseudo-random sign (1 or -1)
|
|
41
|
+
*
|
|
42
|
+
* @param {string} seed
|
|
43
|
+
* @param {number} [probability=0.5] Probability to get 1
|
|
44
|
+
* @returns {number} Either 1 or -1
|
|
45
|
+
*/
|
|
46
|
+
randomSign(seed: string, probability?: number): number;
|
|
47
|
+
/**
|
|
48
|
+
* Generate a pseudo-random floating-point number within a specified range
|
|
49
|
+
*
|
|
50
|
+
* @param {string} seed
|
|
51
|
+
* @param {number} [min=0] Minimum boundary
|
|
52
|
+
* @param {number} [max=1] Maximum boundary
|
|
53
|
+
* @param {number} [precision=2] Number of digits after the decimal point
|
|
54
|
+
* @returns {number} Generated float
|
|
55
|
+
*/
|
|
56
|
+
randomFloat(seed: string, min?: number, max?: number, precision?: number): number;
|
|
57
|
+
/**
|
|
58
|
+
* Generate a pseudo-random integer number within a specified range
|
|
59
|
+
*
|
|
60
|
+
* @param {string} seed
|
|
61
|
+
* @param {number} min Minimum boundary
|
|
62
|
+
* @param {number} max Maximum boundary
|
|
63
|
+
* @returns {number} Generated integer
|
|
64
|
+
*/
|
|
65
|
+
randomInt(seed: string, min: number, max: number): number;
|
|
66
|
+
/**
|
|
67
|
+
* Generate a pseudo-random hexadecimal color
|
|
68
|
+
*
|
|
69
|
+
* @param {string} seed
|
|
70
|
+
* @returns {string} Generated hexadecimal color
|
|
71
|
+
*/
|
|
72
|
+
randomHexColor(seed: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* Pick a pseudo-random item from a given array
|
|
75
|
+
*
|
|
76
|
+
* @param {string} seed
|
|
77
|
+
* @param {T[]} array Array to pick the item from
|
|
78
|
+
* @returns {T|undefined} Random item picked
|
|
79
|
+
*/
|
|
80
|
+
randomItem<T = unknown>(seed: string, array: T[]): T | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Pick a pseudo-random property value from a given object
|
|
83
|
+
*
|
|
84
|
+
* @param {string} seed
|
|
85
|
+
* @param {object} object Object to pick the property from
|
|
86
|
+
* @returns {T|undefined} Random item picked
|
|
87
|
+
*/
|
|
88
|
+
randomObjectProperty<T = unknown>(seed: string, object: Record<string, T>): T | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* Select a pseudo-random index from an array of weighted items
|
|
91
|
+
*
|
|
92
|
+
* @param {string} seed
|
|
93
|
+
* @param {number[]} weights Array of weights
|
|
94
|
+
* @returns {number} Random index based on weights
|
|
95
|
+
*/
|
|
96
|
+
randomIndex(seed: string, weights: number[]): number;
|
|
97
|
+
/**
|
|
98
|
+
* Get the PRNG algorithm function by its name
|
|
99
|
+
*
|
|
100
|
+
* @param {AlgorithmName} algorithmName Algorithm name
|
|
101
|
+
* @returns {Function} PRNG algorithm function
|
|
102
|
+
*/
|
|
103
|
+
protected getAlgorithmByName(algorithmName: AlgorithmName): (...args: number[]) => number;
|
|
104
|
+
}
|
|
105
|
+
declare const prng: PRNG;
|
|
106
|
+
export default prng;
|
package/lib/prng.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { cyrb128, jsf32, mulberry32, sfc32, splitmix32, xoshiro128ss } from './utils';
|
|
2
|
+
import { AlgorithmName } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Utility class for generating pseudo-random values
|
|
5
|
+
*
|
|
6
|
+
* @class PRNG
|
|
7
|
+
* @exports
|
|
8
|
+
*/
|
|
9
|
+
var PRNG = /** @class */ (function () {
|
|
10
|
+
function PRNG() {
|
|
11
|
+
this.seed = '';
|
|
12
|
+
this.algorithm = splitmix32;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Set this PRNG seed
|
|
16
|
+
*
|
|
17
|
+
* @param {string} seed
|
|
18
|
+
*/
|
|
19
|
+
PRNG.prototype.setSeed = function (seed) {
|
|
20
|
+
this.seed = seed;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Set this PRNG algorithm
|
|
24
|
+
*
|
|
25
|
+
* @param {AlgorithmName} algorithmName Algorithm name
|
|
26
|
+
*/
|
|
27
|
+
PRNG.prototype.setAlgorithm = function (algorithmName) {
|
|
28
|
+
this.algorithm = this.getAlgorithmByName(algorithmName);
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Generate a pseudo-random number in the interval [0, 1]
|
|
32
|
+
* PRNG equivalent of `Math.random()`
|
|
33
|
+
*
|
|
34
|
+
* @param {string} seed
|
|
35
|
+
* @returns {number}
|
|
36
|
+
*/
|
|
37
|
+
PRNG.prototype.random = function (seed) {
|
|
38
|
+
var hashes = cyrb128(this.seed + seed);
|
|
39
|
+
return this.algorithm.apply(this, hashes);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Generate a pseudo-random boolean (true or false)
|
|
43
|
+
*
|
|
44
|
+
* @param {string} seed
|
|
45
|
+
* @param {number} [probability=0.5] Probability to get `true`
|
|
46
|
+
* @returns {boolean} Either `true` or `false`
|
|
47
|
+
*/
|
|
48
|
+
PRNG.prototype.randomBoolean = function (seed, probability) {
|
|
49
|
+
if (probability === void 0) { probability = 0.5; }
|
|
50
|
+
return this.random(seed) < probability;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Generate a pseudo-random sign (1 or -1)
|
|
54
|
+
*
|
|
55
|
+
* @param {string} seed
|
|
56
|
+
* @param {number} [probability=0.5] Probability to get 1
|
|
57
|
+
* @returns {number} Either 1 or -1
|
|
58
|
+
*/
|
|
59
|
+
PRNG.prototype.randomSign = function (seed, probability) {
|
|
60
|
+
if (probability === void 0) { probability = 0.5; }
|
|
61
|
+
return this.randomBoolean(seed, probability) ? 1 : -1;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Generate a pseudo-random floating-point number within a specified range
|
|
65
|
+
*
|
|
66
|
+
* @param {string} seed
|
|
67
|
+
* @param {number} [min=0] Minimum boundary
|
|
68
|
+
* @param {number} [max=1] Maximum boundary
|
|
69
|
+
* @param {number} [precision=2] Number of digits after the decimal point
|
|
70
|
+
* @returns {number} Generated float
|
|
71
|
+
*/
|
|
72
|
+
PRNG.prototype.randomFloat = function (seed, min, max, precision) {
|
|
73
|
+
if (min === void 0) { min = 0; }
|
|
74
|
+
if (max === void 0) { max = 1; }
|
|
75
|
+
if (precision === void 0) { precision = 2; }
|
|
76
|
+
return parseFloat(Math.min(min + this.random(seed) * (max - min), max).toFixed(precision));
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Generate a pseudo-random integer number within a specified range
|
|
80
|
+
*
|
|
81
|
+
* @param {string} seed
|
|
82
|
+
* @param {number} min Minimum boundary
|
|
83
|
+
* @param {number} max Maximum boundary
|
|
84
|
+
* @returns {number} Generated integer
|
|
85
|
+
*/
|
|
86
|
+
PRNG.prototype.randomInt = function (seed, min, max) {
|
|
87
|
+
return Math.floor(this.random(seed) * (max - min + 1) + min);
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Generate a pseudo-random hexadecimal color
|
|
91
|
+
*
|
|
92
|
+
* @param {string} seed
|
|
93
|
+
* @returns {string} Generated hexadecimal color
|
|
94
|
+
*/
|
|
95
|
+
PRNG.prototype.randomHexColor = function (seed) {
|
|
96
|
+
return '#' + ('00000' + ((this.random(seed) * (1 << 24)) | 0).toString(16)).slice(-6);
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Pick a pseudo-random item from a given array
|
|
100
|
+
*
|
|
101
|
+
* @param {string} seed
|
|
102
|
+
* @param {T[]} array Array to pick the item from
|
|
103
|
+
* @returns {T|undefined} Random item picked
|
|
104
|
+
*/
|
|
105
|
+
PRNG.prototype.randomItem = function (seed, array) {
|
|
106
|
+
if (array.length === 0)
|
|
107
|
+
return undefined;
|
|
108
|
+
return array[this.randomInt(seed, 0, array.length - 1)];
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Pick a pseudo-random property value from a given object
|
|
112
|
+
*
|
|
113
|
+
* @param {string} seed
|
|
114
|
+
* @param {object} object Object to pick the property from
|
|
115
|
+
* @returns {T|undefined} Random item picked
|
|
116
|
+
*/
|
|
117
|
+
PRNG.prototype.randomObjectProperty = function (seed, object) {
|
|
118
|
+
var keys = Object.keys(object);
|
|
119
|
+
var key = this.randomItem(seed, keys);
|
|
120
|
+
if (key && object.hasOwnProperty(key)) {
|
|
121
|
+
return object[key];
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Select a pseudo-random index from an array of weighted items
|
|
126
|
+
*
|
|
127
|
+
* @param {string} seed
|
|
128
|
+
* @param {number[]} weights Array of weights
|
|
129
|
+
* @returns {number} Random index based on weights
|
|
130
|
+
*/
|
|
131
|
+
PRNG.prototype.randomIndex = function (seed, weights) {
|
|
132
|
+
if (weights.length === 0)
|
|
133
|
+
return -1;
|
|
134
|
+
var totalWeight = 0;
|
|
135
|
+
for (var _i = 0, weights_1 = weights; _i < weights_1.length; _i++) {
|
|
136
|
+
var weight_1 = weights_1[_i];
|
|
137
|
+
totalWeight += weight_1;
|
|
138
|
+
}
|
|
139
|
+
if (totalWeight <= 0)
|
|
140
|
+
console.warn('PRNG.randomIndex()', 'Weights must sum to > 0', totalWeight);
|
|
141
|
+
var weight = this.random(seed) * totalWeight;
|
|
142
|
+
for (var i = 0; i < weights.length; i++) {
|
|
143
|
+
if (weight < weights[i])
|
|
144
|
+
return i;
|
|
145
|
+
weight -= weights[i];
|
|
146
|
+
}
|
|
147
|
+
return 0;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Get the PRNG algorithm function by its name
|
|
151
|
+
*
|
|
152
|
+
* @param {AlgorithmName} algorithmName Algorithm name
|
|
153
|
+
* @returns {Function} PRNG algorithm function
|
|
154
|
+
*/
|
|
155
|
+
PRNG.prototype.getAlgorithmByName = function (algorithmName) {
|
|
156
|
+
switch (algorithmName) {
|
|
157
|
+
case AlgorithmName.jsf32:
|
|
158
|
+
return jsf32;
|
|
159
|
+
case AlgorithmName.mulberry32:
|
|
160
|
+
return mulberry32;
|
|
161
|
+
case AlgorithmName.sfc32:
|
|
162
|
+
return sfc32;
|
|
163
|
+
case AlgorithmName.splitmix32:
|
|
164
|
+
return splitmix32;
|
|
165
|
+
case AlgorithmName.xoshiro128ss:
|
|
166
|
+
return xoshiro128ss;
|
|
167
|
+
default:
|
|
168
|
+
return splitmix32;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
return PRNG;
|
|
172
|
+
}());
|
|
173
|
+
export { PRNG };
|
|
174
|
+
var prng = new PRNG();
|
|
175
|
+
export default prng;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"program":{"fileNames":["../../../node_modules/typescript/lib/lib.es5.d.ts","../../../node_modules/typescript/lib/lib.es2015.d.ts","../../../node_modules/typescript/lib/lib.es2016.d.ts","../../../node_modules/typescript/lib/lib.es2017.d.ts","../../../node_modules/typescript/lib/lib.es2018.d.ts","../../../node_modules/typescript/lib/lib.es2019.d.ts","../../../node_modules/typescript/lib/lib.es2020.d.ts","../../../node_modules/typescript/lib/lib.es2021.d.ts","../../../node_modules/typescript/lib/lib.es2022.d.ts","../../../node_modules/typescript/lib/lib.es2023.d.ts","../../../node_modules/typescript/lib/lib.esnext.d.ts","../../../node_modules/typescript/lib/lib.dom.d.ts","../../../node_modules/typescript/lib/lib.es2015.core.d.ts","../../../node_modules/typescript/lib/lib.es2015.collection.d.ts","../../../node_modules/typescript/lib/lib.es2015.generator.d.ts","../../../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../../node_modules/typescript/lib/lib.es2015.promise.d.ts","../../../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../../node_modules/typescript/lib/lib.es2016.intl.d.ts","../../../node_modules/typescript/lib/lib.es2017.date.d.ts","../../../node_modules/typescript/lib/lib.es2017.object.d.ts","../../../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2017.string.d.ts","../../../node_modules/typescript/lib/lib.es2017.intl.d.ts","../../../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../../node_modules/typescript/lib/lib.es2018.intl.d.ts","../../../node_modules/typescript/lib/lib.es2018.promise.d.ts","../../../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../../node_modules/typescript/lib/lib.es2019.array.d.ts","../../../node_modules/typescript/lib/lib.es2019.object.d.ts","../../../node_modules/typescript/lib/lib.es2019.string.d.ts","../../../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../../node_modules/typescript/lib/lib.es2019.intl.d.ts","../../../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../../node_modules/typescript/lib/lib.es2020.date.d.ts","../../../node_modules/typescript/lib/lib.es2020.promise.d.ts","../../../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2020.string.d.ts","../../../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../../node_modules/typescript/lib/lib.es2020.intl.d.ts","../../../node_modules/typescript/lib/lib.es2020.number.d.ts","../../../node_modules/typescript/lib/lib.es2021.promise.d.ts","../../../node_modules/typescript/lib/lib.es2021.string.d.ts","../../../node_modules/typescript/lib/lib.es2021.weakref.d.ts","../../../node_modules/typescript/lib/lib.es2021.intl.d.ts","../../../node_modules/typescript/lib/lib.es2022.array.d.ts","../../../node_modules/typescript/lib/lib.es2022.error.d.ts","../../../node_modules/typescript/lib/lib.es2022.intl.d.ts","../../../node_modules/typescript/lib/lib.es2022.object.d.ts","../../../node_modules/typescript/lib/lib.es2022.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2022.string.d.ts","../../../node_modules/typescript/lib/lib.es2022.regexp.d.ts","../../../node_modules/typescript/lib/lib.es2023.array.d.ts","../../../node_modules/typescript/lib/lib.es2023.collection.d.ts","../../../node_modules/typescript/lib/lib.esnext.collection.d.ts","../../../node_modules/typescript/lib/lib.esnext.intl.d.ts","../../../node_modules/typescript/lib/lib.esnext.disposable.d.ts","../../../node_modules/typescript/lib/lib.esnext.promise.d.ts","../../../node_modules/typescript/lib/lib.esnext.decorators.d.ts","../../../node_modules/typescript/lib/lib.esnext.object.d.ts","../../../node_modules/typescript/lib/lib.decorators.d.ts","../../../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../src/utils.ts","../src/types.ts","../src/prng.ts","../src/index.ts"],"fileInfos":[{"version":"824cb491a40f7e8fdeb56f1df5edf91b23f3e3ee6b4cde84d4a99be32338faee","affectsGlobalScope":true},"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","3facaf05f0c5fc569c5649dd359892c98a85557e3e0c847964caeb67076f4d75","9a68c0c07ae2fa71b44384a839b7b8d81662a236d4b9ac30916718f7510b1b2d","5e1c4c362065a6b95ff952c0eab010f04dcd2c3494e813b493ecfd4fcb9fc0d8","68d73b4a11549f9c0b7d352d10e91e5dca8faa3322bfb77b661839c42b1ddec7","5efce4fc3c29ea84e8928f97adec086e3dc876365e0982cc8479a07954a3efd4","feecb1be483ed332fad555aff858affd90a48ab19ba7272ee084704eb7167569","5514e54f17d6d74ecefedc73c504eadffdeda79c7ea205cf9febead32d45c4bc","1c0cdb8dc619bc549c3e5020643e7cf7ae7940058e8c7e5aefa5871b6d86f44b","886e50ef125efb7878f744e86908884c0133e7a6d9d80013f421b0cd8fb2af94",{"version":"87d693a4920d794a73384b3c779cadcb8548ac6945aa7a925832fe2418c9527a","affectsGlobalScope":true},{"version":"138fb588d26538783b78d1e3b2c2cc12d55840b97bf5e08bca7f7a174fbe2f17","affectsGlobalScope":true},{"version":"dc2df20b1bcdc8c2d34af4926e2c3ab15ffe1160a63e58b7e09833f616efff44","affectsGlobalScope":true},{"version":"4443e68b35f3332f753eacc66a04ac1d2053b8b035a0e0ac1d455392b5e243b3","affectsGlobalScope":true},{"version":"bc47685641087c015972a3f072480889f0d6c65515f12bd85222f49a98952ed7","affectsGlobalScope":true},{"version":"0dc1e7ceda9b8b9b455c3a2d67b0412feab00bd2f66656cd8850e8831b08b537","affectsGlobalScope":true},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true},{"version":"4ff2a353abf8a80ee399af572debb8faab2d33ad38c4b4474cff7f26e7653b8d","affectsGlobalScope":true},{"version":"93495ff27b8746f55d19fcbcdbaccc99fd95f19d057aed1bd2c0cafe1335fbf0","affectsGlobalScope":true},{"version":"6fc23bb8c3965964be8c597310a2878b53a0306edb71d4b5a4dfe760186bcc01","affectsGlobalScope":true},{"version":"ea011c76963fb15ef1cdd7ce6a6808b46322c527de2077b6cfdf23ae6f5f9ec7","affectsGlobalScope":true},{"version":"38f0219c9e23c915ef9790ab1d680440d95419ad264816fa15009a8851e79119","affectsGlobalScope":true},{"version":"bb42a7797d996412ecdc5b2787720de477103a0b2e53058569069a0e2bae6c7e","affectsGlobalScope":true},{"version":"4738f2420687fd85629c9efb470793bb753709c2379e5f85bc1815d875ceadcd","affectsGlobalScope":true},{"version":"2f11ff796926e0832f9ae148008138ad583bd181899ab7dd768a2666700b1893","affectsGlobalScope":true},{"version":"4de680d5bb41c17f7f68e0419412ca23c98d5749dcaaea1896172f06435891fc","affectsGlobalScope":true},{"version":"9fc46429fbe091ac5ad2608c657201eb68b6f1b8341bd6d670047d32ed0a88fa","affectsGlobalScope":true},{"version":"61c37c1de663cf4171e1192466e52c7a382afa58da01b1dc75058f032ddf0839","affectsGlobalScope":true},{"version":"b541a838a13f9234aba650a825393ffc2292dc0fc87681a5d81ef0c96d281e7a","affectsGlobalScope":true},{"version":"b20fe0eca9a4e405f1a5ae24a2b3290b37cf7f21eba6cbe4fc3fab979237d4f3","affectsGlobalScope":true},{"version":"811ec78f7fefcabbda4bfa93b3eb67d9ae166ef95f9bff989d964061cbf81a0c","affectsGlobalScope":true},{"version":"717937616a17072082152a2ef351cb51f98802fb4b2fdabd32399843875974ca","affectsGlobalScope":true},{"version":"d7e7d9b7b50e5f22c915b525acc5a49a7a6584cf8f62d0569e557c5cfc4b2ac2","affectsGlobalScope":true},{"version":"71c37f4c9543f31dfced6c7840e068c5a5aacb7b89111a4364b1d5276b852557","affectsGlobalScope":true},{"version":"576711e016cf4f1804676043e6a0a5414252560eb57de9faceee34d79798c850","affectsGlobalScope":true},{"version":"89c1b1281ba7b8a96efc676b11b264de7a8374c5ea1e6617f11880a13fc56dc6","affectsGlobalScope":true},{"version":"49ed889be54031e1044af0ad2c603d627b8bda8b50c1a68435fe85583901d072","affectsGlobalScope":true},{"version":"e93d098658ce4f0c8a0779e6cab91d0259efb88a318137f686ad76f8410ca270","affectsGlobalScope":true},{"version":"063600664504610fe3e99b717a1223f8b1900087fab0b4cad1496a114744f8df","affectsGlobalScope":true},{"version":"934019d7e3c81950f9a8426d093458b65d5aff2c7c1511233c0fd5b941e608ab","affectsGlobalScope":true},{"version":"bf14a426dbbf1022d11bd08d6b8e709a2e9d246f0c6c1032f3b2edb9a902adbe","affectsGlobalScope":true},{"version":"5e07ed3809d48205d5b985642a59f2eba47c402374a7cf8006b686f79efadcbd","affectsGlobalScope":true},{"version":"2b72d528b2e2fe3c57889ca7baef5e13a56c957b946906d03767c642f386bbc3","affectsGlobalScope":true},{"version":"8073890e29d2f46fdbc19b8d6d2eb9ea58db9a2052f8640af20baff9afbc8640","affectsGlobalScope":true},{"version":"368af93f74c9c932edd84c58883e736c9e3d53cec1fe24c0b0ff451f529ceab1","affectsGlobalScope":true},{"version":"af3dd424cf267428f30ccfc376f47a2c0114546b55c44d8c0f1d57d841e28d74","affectsGlobalScope":true},{"version":"995c005ab91a498455ea8dfb63aa9f83fa2ea793c3d8aa344be4a1678d06d399","affectsGlobalScope":true},{"version":"51e547984877a62227042850456de71a5c45e7fe86b7c975c6e68896c86fa23b","affectsGlobalScope":true},{"version":"956d27abdea9652e8368ce029bb1e0b9174e9678a273529f426df4b3d90abd60","affectsGlobalScope":true},{"version":"4fa6ed14e98aa80b91f61b9805c653ee82af3502dc21c9da5268d3857772ca05","affectsGlobalScope":true},{"version":"e6633e05da3ff36e6da2ec170d0d03ccf33de50ca4dc6f5aeecb572cedd162fb","affectsGlobalScope":true},{"version":"d8670852241d4c6e03f2b89d67497a4bbefe29ecaa5a444e2c11a9b05e6fccc6","affectsGlobalScope":true},{"version":"8444af78980e3b20b49324f4a16ba35024fef3ee069a0eb67616ea6ca821c47a","affectsGlobalScope":true},{"version":"caccc56c72713969e1cfe5c3d44e5bab151544d9d2b373d7dbe5a1e4166652be","affectsGlobalScope":true},{"version":"3287d9d085fbd618c3971944b65b4be57859f5415f495b33a6adc994edd2f004","affectsGlobalScope":true},{"version":"50d53ccd31f6667aff66e3d62adf948879a3a16f05d89882d1188084ee415bbc","affectsGlobalScope":true},{"version":"08a58483392df5fcc1db57d782e87734f77ae9eab42516028acbfe46f29a3ef7","affectsGlobalScope":true},{"version":"436aaf437562f276ec2ddbee2f2cdedac7664c1e4c1d2c36839ddd582eeb3d0a","affectsGlobalScope":true},{"version":"b1cb28af0c891c8c96b2d6b7be76bd394fddcfdb4709a20ba05a7c1605eea0f9","affectsGlobalScope":true},{"version":"13f6e6380c78e15e140243dc4be2fa546c287c6d61f4729bc2dd7cf449605471","affectsGlobalScope":true},{"version":"15b98a533864d324e5f57cd3cfc0579b231df58c1c0f6063ea0fcb13c3c74ff9","affectsGlobalScope":true},{"version":"ac77cb3e8c6d3565793eb90a8373ee8033146315a3dbead3bde8db5eaf5e5ec6","affectsGlobalScope":true},{"version":"d4b1d2c51d058fc21ec2629fff7a76249dec2e36e12960ea056e3ef89174080f","affectsGlobalScope":true},{"version":"2fef54945a13095fdb9b84f705f2b5994597640c46afeb2ce78352fab4cb3279","affectsGlobalScope":true},{"version":"33358442698bb565130f52ba79bfd3d4d484ac85fe33f3cb1759c54d18201393","affectsGlobalScope":true},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true},{"version":"f4c26efe8572cfb4efa528095e9e23d95fcc793b6aa5ea1303ae1ce5ab7e2093","signature":"1c745d75f93f62d55ff159a6091c469b05be5fad36e4ded4ce249e7bbdb36f33"},{"version":"93547df89e896690ccb6c0653ca32cccda2d1164a0ce6fe9055f80f1092b1f68","signature":"0c5524a62612d2e2e02057c09627d6f6044b2547a730db1d8d131453bf5d843a"},{"version":"5234c9a87ed568ccc5a44bcb49c1b2a5c36b5f10b892f67fadf7c0c3b028babb","signature":"868f03dec128c24d6e235c56f967691267351be6f40e27fc8cfd6f69ad080d40"},{"version":"e4035c130e9e1e9c9462141cf3af38879f74d9653e70c7585e0cec14a3fee8a4","signature":"f9ca5cc1b894f1a19cbb55135cbfa308ad994ef06e1412e04ff1796272a93b89"}],"root":[[69,72]],"options":{"allowJs":true,"allowSyntheticDefaultImports":true,"declaration":true,"esModuleInterop":true,"experimentalDecorators":true,"module":99,"noUnusedLocals":true,"noUnusedParameters":true,"outDir":"./","skipLibCheck":true,"strict":true,"target":1},"fileIdsList":[[70,71],[69,70],[70]],"referencedMap":[[72,1],[71,2]],"exportedModulesMap":[[72,1],[71,3]],"semanticDiagnosticsPerFile":[67,68,12,14,13,2,15,16,17,18,19,20,21,22,3,23,4,24,28,25,26,27,29,30,31,5,32,33,34,35,6,39,36,37,38,40,7,41,46,47,42,43,44,45,8,51,48,49,50,52,9,53,54,55,58,56,57,59,60,10,1,61,11,65,63,62,66,64,72,71,70,69]},"version":"5.4.2"}
|
package/lib/types.d.ts
ADDED
package/lib/types.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export var AlgorithmName;
|
|
2
|
+
(function (AlgorithmName) {
|
|
3
|
+
AlgorithmName["jsf32"] = "jsf32";
|
|
4
|
+
AlgorithmName["mulberry32"] = "mulberry32";
|
|
5
|
+
AlgorithmName["sfc32"] = "sfc32";
|
|
6
|
+
AlgorithmName["splitmix32"] = "splitmix32";
|
|
7
|
+
AlgorithmName["xoshiro128ss"] = "xoshiro128**";
|
|
8
|
+
})(AlgorithmName || (AlgorithmName = {}));
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Produce a 128-bit hash value from a seed
|
|
3
|
+
*
|
|
4
|
+
* @param {string} seed Initial seed state
|
|
5
|
+
* @returns {[number, number, number, number]} Hash numbers
|
|
6
|
+
*/
|
|
7
|
+
export declare function cyrb128(seed: string): [number, number, number, number];
|
|
8
|
+
/**
|
|
9
|
+
* Simple Fast Counter, Generator with a 128-bit state
|
|
10
|
+
*
|
|
11
|
+
* @param {number} a
|
|
12
|
+
* @param {number} b
|
|
13
|
+
* @param {number} c
|
|
14
|
+
* @param {number} d
|
|
15
|
+
* @returns {number} Pseudo-random number
|
|
16
|
+
*/
|
|
17
|
+
export declare function sfc32(a: number, b: number, c: number, d: number): number;
|
|
18
|
+
/**
|
|
19
|
+
* SplitMix32, Generator with a 32-bit state
|
|
20
|
+
*
|
|
21
|
+
* @param {number} a
|
|
22
|
+
* @returns {number} Pseudo-random number
|
|
23
|
+
*/
|
|
24
|
+
export declare function splitmix32(a: number): number;
|
|
25
|
+
/**
|
|
26
|
+
* Mulberry32, Generator with a 32-bit state
|
|
27
|
+
*
|
|
28
|
+
* @param {number} a
|
|
29
|
+
* @returns {number} Pseudo-random number
|
|
30
|
+
*/
|
|
31
|
+
export declare function mulberry32(a: number): number;
|
|
32
|
+
/**
|
|
33
|
+
* Jenkins' Small Fast, Generator with a 32-bit state
|
|
34
|
+
*
|
|
35
|
+
* @param {number} a
|
|
36
|
+
* @returns {number} Pseudo-random number
|
|
37
|
+
*/
|
|
38
|
+
export declare function jsf32(a: number, b: number, c: number, d: number): number;
|
|
39
|
+
/**
|
|
40
|
+
* xoshiro128**, Generator with a 128-bit state
|
|
41
|
+
*
|
|
42
|
+
* @param {number} a
|
|
43
|
+
* @returns {number} Pseudo-random number
|
|
44
|
+
*/
|
|
45
|
+
export declare function xoshiro128ss(a: number, b: number, c: number, d: number): number;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Produce a 128-bit hash value from a seed
|
|
3
|
+
*
|
|
4
|
+
* @param {string} seed Initial seed state
|
|
5
|
+
* @returns {[number, number, number, number]} Hash numbers
|
|
6
|
+
*/
|
|
7
|
+
export function cyrb128(seed) {
|
|
8
|
+
var h1 = 1779033703;
|
|
9
|
+
var h2 = 3144134277;
|
|
10
|
+
var h3 = 1013904242;
|
|
11
|
+
var h4 = 2773480762;
|
|
12
|
+
for (var i = 0, k = void 0; i < seed.length; i++) {
|
|
13
|
+
k = seed.charCodeAt(i);
|
|
14
|
+
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
|
|
15
|
+
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
|
|
16
|
+
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
|
|
17
|
+
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
|
|
18
|
+
}
|
|
19
|
+
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
|
|
20
|
+
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
|
|
21
|
+
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
|
|
22
|
+
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
|
|
23
|
+
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0];
|
|
24
|
+
}
|
|
25
|
+
// *********************
|
|
26
|
+
// PRNG Algorithms
|
|
27
|
+
// *********************
|
|
28
|
+
/**
|
|
29
|
+
* Simple Fast Counter, Generator with a 128-bit state
|
|
30
|
+
*
|
|
31
|
+
* @param {number} a
|
|
32
|
+
* @param {number} b
|
|
33
|
+
* @param {number} c
|
|
34
|
+
* @param {number} d
|
|
35
|
+
* @returns {number} Pseudo-random number
|
|
36
|
+
*/
|
|
37
|
+
export function sfc32(a, b, c, d) {
|
|
38
|
+
a >>>= 0;
|
|
39
|
+
b >>>= 0;
|
|
40
|
+
c >>>= 0;
|
|
41
|
+
d >>>= 0;
|
|
42
|
+
var t = (a + b) | 0;
|
|
43
|
+
a = b ^ (b >>> 9);
|
|
44
|
+
b = (c + (c << 3)) | 0;
|
|
45
|
+
c = (c << 21) | (c >>> 11);
|
|
46
|
+
d = (d + 1) | 0;
|
|
47
|
+
t = (t + d) | 0;
|
|
48
|
+
c = (c + t) | 0;
|
|
49
|
+
return (t >>> 0) / 4294967296;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* SplitMix32, Generator with a 32-bit state
|
|
53
|
+
*
|
|
54
|
+
* @param {number} a
|
|
55
|
+
* @returns {number} Pseudo-random number
|
|
56
|
+
*/
|
|
57
|
+
export function splitmix32(a) {
|
|
58
|
+
a |= 0;
|
|
59
|
+
a = (a + 0x9e3779b9) | 0;
|
|
60
|
+
var t = a ^ (a >>> 16);
|
|
61
|
+
t = Math.imul(t, 0x21f0aaad);
|
|
62
|
+
t = t ^ (t >>> 15);
|
|
63
|
+
t = Math.imul(t, 0x735a2d97);
|
|
64
|
+
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Mulberry32, Generator with a 32-bit state
|
|
68
|
+
*
|
|
69
|
+
* @param {number} a
|
|
70
|
+
* @returns {number} Pseudo-random number
|
|
71
|
+
*/
|
|
72
|
+
export function mulberry32(a) {
|
|
73
|
+
var t = (a += 0x6d2b79f5);
|
|
74
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
75
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
76
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Jenkins' Small Fast, Generator with a 32-bit state
|
|
80
|
+
*
|
|
81
|
+
* @param {number} a
|
|
82
|
+
* @returns {number} Pseudo-random number
|
|
83
|
+
*/
|
|
84
|
+
export function jsf32(a, b, c, d) {
|
|
85
|
+
a |= 0;
|
|
86
|
+
b |= 0;
|
|
87
|
+
c |= 0;
|
|
88
|
+
d |= 0;
|
|
89
|
+
var t = (a - ((b << 27) | (b >>> 5))) | 0;
|
|
90
|
+
a = b ^ ((c << 17) | (c >>> 15));
|
|
91
|
+
b = (c + d) | 0;
|
|
92
|
+
c = (d + t) | 0;
|
|
93
|
+
d = (a + t) | 0;
|
|
94
|
+
return (d >>> 0) / 4294967296;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* xoshiro128**, Generator with a 128-bit state
|
|
98
|
+
*
|
|
99
|
+
* @param {number} a
|
|
100
|
+
* @returns {number} Pseudo-random number
|
|
101
|
+
*/
|
|
102
|
+
export function xoshiro128ss(a, b, c, d) {
|
|
103
|
+
var t = b << 9;
|
|
104
|
+
var r = a * 5;
|
|
105
|
+
r = ((r << 7) | (r >>> 25)) * 9;
|
|
106
|
+
c ^= a;
|
|
107
|
+
d ^= b;
|
|
108
|
+
b ^= c;
|
|
109
|
+
a ^= d;
|
|
110
|
+
c ^= t;
|
|
111
|
+
d = (d << 11) | (d >>> 21);
|
|
112
|
+
return (r >>> 0) / 4294967296;
|
|
113
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toosoon-prng",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "This project provides PRNG functions for generating pseudo-random values using a seed-based approach and various algorithms",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=16"
|
|
8
|
+
},
|
|
5
9
|
"main": "./lib/index.js",
|
|
6
10
|
"types": "./lib/index.d.ts",
|
|
7
11
|
"scripts": {
|
|
@@ -27,7 +31,7 @@
|
|
|
27
31
|
"url": "https://github.com/toosoon-dev/toosoon-prng/issues"
|
|
28
32
|
},
|
|
29
33
|
"homepage": "https://github.com/toosoon-dev/toosoon-prng#readme",
|
|
30
|
-
"
|
|
31
|
-
"typescript": "
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"typescript": "5.x"
|
|
32
36
|
}
|
|
33
37
|
}
|
package/tsconfig.json
CHANGED
|
@@ -9,10 +9,8 @@
|
|
|
9
9
|
"moduleResolution": "Bundler",
|
|
10
10
|
"lib": ["DOM", "ESNext"],
|
|
11
11
|
"allowJs": true,
|
|
12
|
-
"
|
|
12
|
+
"checkJs": true,
|
|
13
13
|
"declaration": true,
|
|
14
|
-
"esModuleInterop": true,
|
|
15
|
-
"experimentalDecorators": true,
|
|
16
14
|
"forceConsistentCasingInFileNames": true,
|
|
17
15
|
"isolatedModules": true,
|
|
18
16
|
"incremental": true,
|
package/src/prng.ts
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { cyrb128, jsf32, mulberry32, sfc32, splitmix32, xoshiro128ss } from './utils';
|
|
2
|
-
import { AlgorithmName } from './types';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Utility class for generating pseudo-random values
|
|
6
|
-
*
|
|
7
|
-
* @class PRNG
|
|
8
|
-
* @exports
|
|
9
|
-
*/
|
|
10
|
-
export class PRNG {
|
|
11
|
-
public seed: string = '';
|
|
12
|
-
public algorithm: (...args: number[]) => number = splitmix32;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Set this PRNG seed
|
|
16
|
-
*
|
|
17
|
-
* @param {string} seed
|
|
18
|
-
*/
|
|
19
|
-
public setSeed(seed: string): void {
|
|
20
|
-
this.seed = seed;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Set this PRNG algorithm
|
|
25
|
-
*
|
|
26
|
-
* @param {AlgorithmName} algorithmName Algorithm name
|
|
27
|
-
*/
|
|
28
|
-
public setAlgorithm(algorithmName: AlgorithmName): void {
|
|
29
|
-
this.algorithm = this.getAlgorithmByName(algorithmName);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Generate a pseudo-random number in the interval [0, 1]
|
|
34
|
-
* PRNG equivalent of `Math.random()`
|
|
35
|
-
*
|
|
36
|
-
* @param {string} seed
|
|
37
|
-
* @returns {number}
|
|
38
|
-
*/
|
|
39
|
-
public random(seed: string): number {
|
|
40
|
-
const hashes = cyrb128(this.seed + seed);
|
|
41
|
-
return this.algorithm(...hashes);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Generate a pseudo-random boolean (true or false)
|
|
46
|
-
*
|
|
47
|
-
* @param {string} seed
|
|
48
|
-
* @param {number} [probability=0.5] Probability to get `true`
|
|
49
|
-
* @returns {boolean} Either `true` or `false`
|
|
50
|
-
*/
|
|
51
|
-
public randomBoolean(seed: string, probability: number = 0.5): boolean {
|
|
52
|
-
return this.random(seed) < probability;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Generate a pseudo-random sign (1 or -1)
|
|
57
|
-
*
|
|
58
|
-
* @param {string} seed
|
|
59
|
-
* @param {number} [probability=0.5] Probability to get 1
|
|
60
|
-
* @returns {number} Either 1 or -1
|
|
61
|
-
*/
|
|
62
|
-
public randomSign(seed: string, probability: number = 0.5): number {
|
|
63
|
-
return this.randomBoolean(seed, probability) ? 1 : -1;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Generate a pseudo-random floating-point number within a specified range
|
|
68
|
-
*
|
|
69
|
-
* @param {string} seed
|
|
70
|
-
* @param {number} [min=0] Minimum boundary
|
|
71
|
-
* @param {number} [max=1] Maximum boundary
|
|
72
|
-
* @param {number} [precision=2] Number of digits after the decimal point
|
|
73
|
-
* @returns {number} Generated float
|
|
74
|
-
*/
|
|
75
|
-
public randomFloat(seed: string, min: number = 0, max: number = 1, precision: number = 2): number {
|
|
76
|
-
return parseFloat(Math.min(min + this.random(seed) * (max - min), max).toFixed(precision));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Generate a pseudo-random integer number within a specified range
|
|
81
|
-
*
|
|
82
|
-
* @param {string} seed
|
|
83
|
-
* @param {number} min Minimum boundary
|
|
84
|
-
* @param {number} max Maximum boundary
|
|
85
|
-
* @returns {number} Generated integer
|
|
86
|
-
*/
|
|
87
|
-
public randomInt(seed: string, min: number, max: number): number {
|
|
88
|
-
return Math.floor(this.random(seed) * (max - min + 1) + min);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Generate a pseudo-random hexadecimal color
|
|
93
|
-
*
|
|
94
|
-
* @param {string} seed
|
|
95
|
-
* @returns {string} Generated hexadecimal color
|
|
96
|
-
*/
|
|
97
|
-
public randomHexColor(seed: string): string {
|
|
98
|
-
return '#' + ('00000' + ((this.random(seed) * (1 << 24)) | 0).toString(16)).slice(-6);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Pick a pseudo-random item from a given array
|
|
103
|
-
*
|
|
104
|
-
* @param {string} seed
|
|
105
|
-
* @param {T[]} array Array to pick the item from
|
|
106
|
-
* @returns {T|undefined} Random item picked
|
|
107
|
-
*/
|
|
108
|
-
public randomItem<T = unknown>(seed: string, array: T[]): T | undefined {
|
|
109
|
-
if (array.length === 0) return undefined;
|
|
110
|
-
return array[this.randomInt(seed, 0, array.length - 1)];
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Pick a pseudo-random property value from a given object
|
|
115
|
-
*
|
|
116
|
-
* @param {string} seed
|
|
117
|
-
* @param {object} object Object to pick the property from
|
|
118
|
-
* @returns {T|undefined} Random item picked
|
|
119
|
-
*/
|
|
120
|
-
public randomObjectProperty<T = unknown>(seed: string, object: Record<string, T>): T | undefined {
|
|
121
|
-
const keys = Object.keys(object);
|
|
122
|
-
const key = this.randomItem(seed, keys);
|
|
123
|
-
if (key && object.hasOwnProperty(key)) {
|
|
124
|
-
return object[key as keyof object];
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Select a pseudo-random index from an array of weighted items
|
|
130
|
-
*
|
|
131
|
-
* @param {string} seed
|
|
132
|
-
* @param {number[]} weights Array of weights
|
|
133
|
-
* @returns {number} Random index based on weights
|
|
134
|
-
*/
|
|
135
|
-
public randomIndex(seed: string, weights: number[]): number {
|
|
136
|
-
if (weights.length === 0) return -1;
|
|
137
|
-
|
|
138
|
-
let totalWeight = 0;
|
|
139
|
-
for (let weight of weights) {
|
|
140
|
-
totalWeight += weight;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (totalWeight <= 0) console.warn('PRNG.randomIndex()', 'Weights must sum to > 0', totalWeight);
|
|
144
|
-
|
|
145
|
-
let weight = this.random(seed) * totalWeight;
|
|
146
|
-
for (let i = 0; i < weights.length; i++) {
|
|
147
|
-
if (weight < weights[i]) return i;
|
|
148
|
-
weight -= weights[i];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return 0;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Get the PRNG algorithm function by its name
|
|
156
|
-
*
|
|
157
|
-
* @param {AlgorithmName} algorithmName Algorithm name
|
|
158
|
-
* @returns {Function} PRNG algorithm function
|
|
159
|
-
*/
|
|
160
|
-
protected getAlgorithmByName(algorithmName: AlgorithmName): (...args: number[]) => number {
|
|
161
|
-
switch (algorithmName) {
|
|
162
|
-
case AlgorithmName.jsf32:
|
|
163
|
-
return jsf32;
|
|
164
|
-
case AlgorithmName.mulberry32:
|
|
165
|
-
return mulberry32;
|
|
166
|
-
case AlgorithmName.sfc32:
|
|
167
|
-
return sfc32;
|
|
168
|
-
case AlgorithmName.splitmix32:
|
|
169
|
-
return splitmix32;
|
|
170
|
-
case AlgorithmName.xoshiro128ss:
|
|
171
|
-
return xoshiro128ss;
|
|
172
|
-
default:
|
|
173
|
-
return splitmix32;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const prng = new PRNG();
|
|
179
|
-
|
|
180
|
-
export default prng;
|
package/src/types.ts
DELETED
package/src/utils.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Produce a 128-bit hash value from a seed
|
|
3
|
-
*
|
|
4
|
-
* @param {string} seed Initial seed state
|
|
5
|
-
* @returns {[number, number, number, number]} Hash numbers
|
|
6
|
-
*/
|
|
7
|
-
export function cyrb128(seed: string): [number, number, number, number] {
|
|
8
|
-
let h1 = 1779033703;
|
|
9
|
-
let h2 = 3144134277;
|
|
10
|
-
let h3 = 1013904242;
|
|
11
|
-
let h4 = 2773480762;
|
|
12
|
-
for (let i = 0, k; i < seed.length; i++) {
|
|
13
|
-
k = seed.charCodeAt(i);
|
|
14
|
-
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
|
|
15
|
-
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
|
|
16
|
-
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
|
|
17
|
-
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
|
|
18
|
-
}
|
|
19
|
-
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
|
|
20
|
-
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
|
|
21
|
-
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
|
|
22
|
-
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
|
|
23
|
-
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// *********************
|
|
27
|
-
// PRNG Algorithms
|
|
28
|
-
// *********************
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Simple Fast Counter, Generator with a 128-bit state
|
|
32
|
-
*
|
|
33
|
-
* @param {number} a
|
|
34
|
-
* @param {number} b
|
|
35
|
-
* @param {number} c
|
|
36
|
-
* @param {number} d
|
|
37
|
-
* @returns {number} Pseudo-random number
|
|
38
|
-
*/
|
|
39
|
-
export function sfc32(a: number, b: number, c: number, d: number): number {
|
|
40
|
-
a >>>= 0;
|
|
41
|
-
b >>>= 0;
|
|
42
|
-
c >>>= 0;
|
|
43
|
-
d >>>= 0;
|
|
44
|
-
let t = (a + b) | 0;
|
|
45
|
-
a = b ^ (b >>> 9);
|
|
46
|
-
b = (c + (c << 3)) | 0;
|
|
47
|
-
c = (c << 21) | (c >>> 11);
|
|
48
|
-
d = (d + 1) | 0;
|
|
49
|
-
t = (t + d) | 0;
|
|
50
|
-
c = (c + t) | 0;
|
|
51
|
-
return (t >>> 0) / 4294967296;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* SplitMix32, Generator with a 32-bit state
|
|
56
|
-
*
|
|
57
|
-
* @param {number} a
|
|
58
|
-
* @returns {number} Pseudo-random number
|
|
59
|
-
*/
|
|
60
|
-
export function splitmix32(a: number): number {
|
|
61
|
-
a |= 0;
|
|
62
|
-
a = (a + 0x9e3779b9) | 0;
|
|
63
|
-
var t = a ^ (a >>> 16);
|
|
64
|
-
t = Math.imul(t, 0x21f0aaad);
|
|
65
|
-
t = t ^ (t >>> 15);
|
|
66
|
-
t = Math.imul(t, 0x735a2d97);
|
|
67
|
-
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Mulberry32, Generator with a 32-bit state
|
|
72
|
-
*
|
|
73
|
-
* @param {number} a
|
|
74
|
-
* @returns {number} Pseudo-random number
|
|
75
|
-
*/
|
|
76
|
-
export function mulberry32(a: number): number {
|
|
77
|
-
let t = (a += 0x6d2b79f5);
|
|
78
|
-
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
79
|
-
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
80
|
-
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Jenkins' Small Fast, Generator with a 32-bit state
|
|
85
|
-
*
|
|
86
|
-
* @param {number} a
|
|
87
|
-
* @returns {number} Pseudo-random number
|
|
88
|
-
*/
|
|
89
|
-
export function jsf32(a: number, b: number, c: number, d: number): number {
|
|
90
|
-
a |= 0;
|
|
91
|
-
b |= 0;
|
|
92
|
-
c |= 0;
|
|
93
|
-
d |= 0;
|
|
94
|
-
let t = (a - ((b << 27) | (b >>> 5))) | 0;
|
|
95
|
-
a = b ^ ((c << 17) | (c >>> 15));
|
|
96
|
-
b = (c + d) | 0;
|
|
97
|
-
c = (d + t) | 0;
|
|
98
|
-
d = (a + t) | 0;
|
|
99
|
-
return (d >>> 0) / 4294967296;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* xoshiro128**, Generator with a 128-bit state
|
|
104
|
-
*
|
|
105
|
-
* @param {number} a
|
|
106
|
-
* @returns {number} Pseudo-random number
|
|
107
|
-
*/
|
|
108
|
-
export function xoshiro128ss(a: number, b: number, c: number, d: number): number {
|
|
109
|
-
let t = b << 9;
|
|
110
|
-
let r = a * 5;
|
|
111
|
-
r = ((r << 7) | (r >>> 25)) * 9;
|
|
112
|
-
c ^= a;
|
|
113
|
-
d ^= b;
|
|
114
|
-
b ^= c;
|
|
115
|
-
a ^= d;
|
|
116
|
-
c ^= t;
|
|
117
|
-
d = (d << 11) | (d >>> 21);
|
|
118
|
-
return (r >>> 0) / 4294967296;
|
|
119
|
-
}
|