toosoon-prng 1.1.0 → 1.2.1
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/README.md +192 -35
- package/lib/controllers.d.ts +13 -13
- package/lib/controllers.js +38 -44
- package/lib/index.d.ts +1 -2
- package/lib/index.js +2 -25
- package/lib/prng.d.ts +96 -6
- package/lib/prng.js +117 -30
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/utils.d.ts +11 -4
- package/lib/utils.js +27 -20
- package/package.json +7 -3
- package/src/controllers.ts +14 -14
- package/src/index.ts +1 -2
- package/src/prng.ts +110 -20
- package/src/utils.ts +23 -7
- package/tsconfig.json +2 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# TOOSOON PRNG
|
|
1
|
+
# TOOSOON Pseudo-Random Number Generator (PRNG)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This project provides a `PRNG` instance and a set of `Controllers` for generating pseudo-random values using a seed-based approach and various algorithms. These controllers are particularly useful for applications requiring deterministic randomization, such as procedural generation or simulations.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -18,158 +18,315 @@ $ npm install toosoon-prng
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
PRNG
|
|
21
|
+
##### PRNG
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
24
|
import prng from 'toosoon-prng';
|
|
25
25
|
|
|
26
|
-
prng.setSeed('
|
|
26
|
+
prng.setSeed('seed');
|
|
27
27
|
console.log(prng.randomFloat('angle', 0, Math.PI)); // Pseudo-random number in the interval [0, PI]
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
PRNG Controllers
|
|
30
|
+
##### PRNG Controllers
|
|
31
31
|
|
|
32
32
|
```ts
|
|
33
|
-
import {
|
|
33
|
+
import { IntController, IntGroupController } from 'toosoon-prng';
|
|
34
34
|
|
|
35
35
|
const config = {
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
count: new IntController('count', 0, Math.PI),
|
|
37
|
+
counts: new IntGroupController('counts', 0, Math.PI)
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
-
config.
|
|
41
|
-
config.
|
|
40
|
+
config.count.addGUI(gui, 0, 10);
|
|
41
|
+
config.counts.addGUI(gui, 5, 10);
|
|
42
42
|
|
|
43
|
-
console.log(config.
|
|
43
|
+
console.log(config.count.getValue()); // Pseudo-random integer in the interval [0, 10]
|
|
44
44
|
|
|
45
45
|
for (let i = 0; i < 5; i++) {
|
|
46
|
-
console.log(config.
|
|
46
|
+
console.log(config.counts.getValueAt(i)); // Pseudo-random integers in the interval [5, 10]
|
|
47
47
|
}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
50
|
## PRNG Functions
|
|
51
51
|
|
|
52
|
+
All PRNG functions have a `seed` argument wich allows you to add a sub-seed string to the `PRNG` instance global `seed` in order to get different pseudo-random outputs while using a PRNG function multiple times.
|
|
53
|
+
|
|
52
54
|
```ts
|
|
53
|
-
|
|
54
|
-
prng.
|
|
55
|
+
prng.setSeed('global-seed');
|
|
56
|
+
console.log(prng.randomBoolean('1')); // Could be true or false
|
|
57
|
+
console.log(prng.randomBoolean('2')); // Could be different from the first pseudo-random value
|
|
55
58
|
```
|
|
56
59
|
|
|
60
|
+
##### setSeed(seed)
|
|
61
|
+
|
|
62
|
+
Set the `PRNG` instance seed.
|
|
63
|
+
|
|
64
|
+
- `seed`
|
|
65
|
+
|
|
57
66
|
```ts
|
|
58
|
-
|
|
59
|
-
prng.setMethod(method: PRNGMethod) => void;
|
|
67
|
+
prng.setSeed(seed: string): void;
|
|
60
68
|
```
|
|
61
69
|
|
|
70
|
+
##### setAlgorithm(algorithm)
|
|
71
|
+
|
|
72
|
+
Set the `PRNG` algorithm for generating pseudo-random values.
|
|
73
|
+
|
|
74
|
+
- `algorithm`: Algorithm name.
|
|
75
|
+
|
|
62
76
|
```ts
|
|
63
|
-
|
|
64
|
-
prng.random(seed: string) => number;
|
|
77
|
+
prng.setAlgorithm(algorithm: Algorithm): void;
|
|
65
78
|
```
|
|
66
79
|
|
|
80
|
+
##### random(seed)
|
|
81
|
+
|
|
82
|
+
Generate a pseudo-random number in the interval [0, 1]. PRNG equivalent of `Math.random()`.
|
|
83
|
+
|
|
84
|
+
- `seed`
|
|
85
|
+
|
|
67
86
|
```ts
|
|
68
|
-
|
|
69
|
-
prng.randomBoolean(seed: string, probability?: number) => boolean;
|
|
87
|
+
prng.random(seed: string): number;
|
|
70
88
|
```
|
|
71
89
|
|
|
90
|
+
##### randomBoolean(seed)
|
|
91
|
+
|
|
92
|
+
Generate a pseudo-random boolean (true or false).
|
|
93
|
+
|
|
94
|
+
- `seed`
|
|
95
|
+
- `[probability=0.5]`: Probability to get `true`.
|
|
96
|
+
|
|
72
97
|
```ts
|
|
73
|
-
|
|
74
|
-
prng.randomSign(seed: string, probability?: number) => number;
|
|
98
|
+
prng.randomBoolean(seed: string, probability?: number): boolean;
|
|
75
99
|
```
|
|
76
100
|
|
|
101
|
+
##### randomSign(seed)
|
|
102
|
+
|
|
103
|
+
Generate a pseudo-random sign (1 or -1).
|
|
104
|
+
|
|
105
|
+
- `seed`
|
|
106
|
+
- `[probability=0.5]`: Probability to get 1.
|
|
107
|
+
|
|
77
108
|
```ts
|
|
78
|
-
|
|
79
|
-
prng.randomFloat(seed: string, min?: number, max?: number1, precision?: number) => number;
|
|
109
|
+
prng.randomSign(seed: string, probability?: number): number;
|
|
80
110
|
```
|
|
81
111
|
|
|
112
|
+
##### randomFloat(seed, min, max)
|
|
113
|
+
|
|
114
|
+
Generate pseudo-random a floating-point number within a specified range.
|
|
115
|
+
|
|
116
|
+
- `seed`
|
|
117
|
+
- `[min=0]`: Minimum boundary.
|
|
118
|
+
- `[max=1]`: Maximum boundary.
|
|
119
|
+
- `[precison=2]`: Number of digits after the decimal point.
|
|
120
|
+
|
|
82
121
|
```ts
|
|
83
|
-
|
|
84
|
-
prng.randomInt(seed: string, min: number, max: number) => void;
|
|
122
|
+
prng.randomFloat(seed: string, min?: number, max?: number1, precision?: number): number;
|
|
85
123
|
```
|
|
86
124
|
|
|
125
|
+
##### randomInt(seed, min, max)
|
|
126
|
+
|
|
127
|
+
Generate pseudo-random integer number within a specified range.
|
|
128
|
+
|
|
129
|
+
- `seed`
|
|
130
|
+
- `min`: Minimum boundary.
|
|
131
|
+
- `max`: Maximum boundary.
|
|
132
|
+
|
|
87
133
|
```ts
|
|
88
|
-
|
|
89
|
-
prng.randomHexColor(seed: string) => string;
|
|
134
|
+
prng.randomInt(seed: string, min: number, max: number): void;
|
|
90
135
|
```
|
|
91
136
|
|
|
137
|
+
##### randomHexColor(seed)
|
|
138
|
+
|
|
139
|
+
Generate a pseudo-random hexadecimal color.
|
|
140
|
+
|
|
141
|
+
- `seed`
|
|
142
|
+
|
|
92
143
|
```ts
|
|
93
|
-
|
|
94
|
-
prng.randomItem<T>(seed: string, array: T[]) => T | undefined;
|
|
144
|
+
prng.randomHexColor(seed: string): string;
|
|
95
145
|
```
|
|
96
146
|
|
|
147
|
+
##### randomItem(seed0 array)
|
|
148
|
+
|
|
149
|
+
Pick a pseudo-random item from a given array.
|
|
150
|
+
|
|
151
|
+
- `seed`
|
|
152
|
+
- `array`: Array to pick the item from.
|
|
153
|
+
|
|
97
154
|
```ts
|
|
98
|
-
|
|
99
|
-
prng.randomObjectProperty(seed: string, object: object) => unknown | undefined
|
|
155
|
+
prng.randomItem<T>(seed: string, array: T[]): T | undefined;
|
|
100
156
|
```
|
|
101
157
|
|
|
158
|
+
##### randomObjectProperty(seed, object)
|
|
159
|
+
|
|
160
|
+
Pick a pseudo-random property value from a given object.
|
|
161
|
+
|
|
162
|
+
- `seed`
|
|
163
|
+
- `object`: Object to pick the property from.
|
|
164
|
+
|
|
102
165
|
```ts
|
|
103
|
-
|
|
104
|
-
prng.randomIndex(seed: string, weights?: number[]) => number;
|
|
166
|
+
prng.randomObjectProperty<T>(seed: string, object: object): T | undefined;
|
|
105
167
|
```
|
|
106
168
|
|
|
107
|
-
|
|
169
|
+
##### randomIndex(seed, weights)
|
|
170
|
+
|
|
171
|
+
Select a pseudo-random index from an array of weighted items.
|
|
172
|
+
|
|
173
|
+
- `seed`
|
|
174
|
+
- `weights`: Array of weights
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
prng.randomIndex(seed: string, weights: number[]): number;
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Controllers
|
|
181
|
+
|
|
182
|
+
### BooleanController
|
|
183
|
+
|
|
184
|
+
Generate pseudo-random boolean values.
|
|
108
185
|
|
|
109
186
|
```ts
|
|
110
187
|
class BooleanController(seed: string, probability?: number);
|
|
111
188
|
```
|
|
112
189
|
|
|
190
|
+
### SignController
|
|
191
|
+
|
|
192
|
+
Generate pseudo-random sign values (-1 or 1).
|
|
193
|
+
|
|
113
194
|
```ts
|
|
114
195
|
class SignController(seed: string, probability?: number);
|
|
115
196
|
```
|
|
116
197
|
|
|
198
|
+
### FloatController
|
|
199
|
+
|
|
200
|
+
Generate pseudo-random floating-point numbers within a specified range.
|
|
201
|
+
|
|
117
202
|
```ts
|
|
118
203
|
class FloatController(seed: string, min?: number, max?: number);
|
|
119
204
|
```
|
|
120
205
|
|
|
206
|
+
### IntController
|
|
207
|
+
|
|
121
208
|
```ts
|
|
122
209
|
class IntController(seed: string, min: number, max: number);
|
|
123
210
|
```
|
|
124
211
|
|
|
212
|
+
### HexColorController
|
|
213
|
+
|
|
214
|
+
Generates pseudo-random hexadecimal color.
|
|
215
|
+
|
|
125
216
|
```ts
|
|
126
217
|
class HexColorController(seed: string);
|
|
127
218
|
```
|
|
128
219
|
|
|
220
|
+
### ItemController
|
|
221
|
+
|
|
222
|
+
Pick a pseudo-random item from a given array.
|
|
223
|
+
|
|
129
224
|
```ts
|
|
130
225
|
class ItemController<T>(seed: string, items: T[]);
|
|
131
226
|
```
|
|
132
227
|
|
|
228
|
+
### ObjectPropertyController
|
|
229
|
+
|
|
230
|
+
Pick a pseudo-random property value from a given object.
|
|
231
|
+
|
|
133
232
|
```ts
|
|
134
233
|
class ObjectPropertyController<T>(seed: string, object: object);
|
|
135
234
|
```
|
|
136
235
|
|
|
236
|
+
### WeightsController
|
|
237
|
+
|
|
238
|
+
Select a pseudo-random index from an array of weighted items.
|
|
239
|
+
|
|
137
240
|
```ts
|
|
138
241
|
class WeightsController<T>(seed: string, items: Array<{ weight: number; value: T }>);
|
|
139
242
|
```
|
|
140
243
|
|
|
244
|
+
## Group Controllers
|
|
245
|
+
|
|
246
|
+
Group Controllers manage multiple instances of individual controllers.
|
|
247
|
+
|
|
248
|
+
### BooleanGroupController
|
|
249
|
+
|
|
250
|
+
Manage multiple [`BooleanController`](###booleancontroller).
|
|
251
|
+
|
|
141
252
|
```ts
|
|
142
253
|
class BooleanGroupController(seed: string, probability: number);
|
|
143
254
|
```
|
|
144
255
|
|
|
256
|
+
### SignGroupController
|
|
257
|
+
|
|
258
|
+
Manage multiple [`SignController`](###signcontroller).
|
|
259
|
+
|
|
145
260
|
```ts
|
|
146
261
|
class SignGroupController(seed: string, probability: number);
|
|
147
262
|
```
|
|
148
263
|
|
|
264
|
+
### FloatGroupController
|
|
265
|
+
|
|
266
|
+
Manage multiple [`FloatController`](###floatcontroller).
|
|
267
|
+
|
|
149
268
|
```ts
|
|
150
269
|
class FloatGroupController(seed: string, min: number, max: number);
|
|
151
270
|
```
|
|
152
271
|
|
|
272
|
+
### IntGroupController
|
|
273
|
+
|
|
274
|
+
Manage multiple [`IntController`](#intcontroller).
|
|
275
|
+
|
|
153
276
|
```ts
|
|
154
277
|
class IntGroupController(seed: string, min: number, max: number);
|
|
155
278
|
```
|
|
156
279
|
|
|
280
|
+
### HexColorGroupController
|
|
281
|
+
|
|
282
|
+
Manage multiple [`HexColorController`](#hexcolorcontroller).
|
|
283
|
+
|
|
157
284
|
```ts
|
|
158
285
|
class HexColorGroupController(seed: string);
|
|
159
286
|
```
|
|
160
287
|
|
|
288
|
+
### ItemGroupController
|
|
289
|
+
|
|
290
|
+
Manage multiple [`ItemController`](#itemcontroller).
|
|
291
|
+
|
|
161
292
|
```ts
|
|
162
293
|
class ItemGroupController<T>(seed: string, items: T[]);
|
|
163
294
|
```
|
|
164
295
|
|
|
296
|
+
### ObjectPropertyGroupController
|
|
297
|
+
|
|
298
|
+
Manage multiple [`ObjectPropertyController`](#objectpropertycontroller).
|
|
299
|
+
|
|
165
300
|
```ts
|
|
166
301
|
class ObjectPropertyGroupController<T>(seed: string, object: object);
|
|
167
302
|
```
|
|
168
303
|
|
|
304
|
+
### WeightsGroupController
|
|
305
|
+
|
|
306
|
+
Manage multiple [`WeightsController`](#weightscontroller).
|
|
307
|
+
|
|
169
308
|
```ts
|
|
170
309
|
class WeightsGroupController<T>(seed: string, items: Array<{ weight: number; value: T }>);
|
|
171
310
|
```
|
|
172
311
|
|
|
312
|
+
## Algorithms
|
|
313
|
+
|
|
314
|
+
By default, the library is using `SplitMix32` algorithm for generating the pseudo-random values but it is possible to change the algorithm used by one of the following:
|
|
315
|
+
|
|
316
|
+
- `jsf32`: Jenkins' Small Fast, Generator with a 32-bit state.
|
|
317
|
+
- `mulberry32`: Mulberry32, Generator with a 32-bit state.
|
|
318
|
+
- `sfc32`: Simple Fast Counter, Generator with a 128-bit state.
|
|
319
|
+
- `splitmix32`: SplitMix32, Generator with a 32-bit state.
|
|
320
|
+
- `xoshiro128ss`: xoshiro128\*\*, Generator with a 128-bit state.
|
|
321
|
+
|
|
322
|
+
**Credits**: [Seeding random number generator in Javascript](https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript)
|
|
323
|
+
|
|
324
|
+
```ts
|
|
325
|
+
import prng, { Algorithm } from 'toosoon-prng';
|
|
326
|
+
|
|
327
|
+
prng.setAlgorithm(Algorithm.sfc32);
|
|
328
|
+
```
|
|
329
|
+
|
|
173
330
|
## License
|
|
174
331
|
|
|
175
332
|
MIT License, see [LICENSE](https://github.com/toosoon-dev/toosoon-prng/tree/master/LICENSE) for details
|
package/lib/controllers.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Gui, GuiController } from 'toosoon-gui';
|
|
2
2
|
export interface PRNGController<T = unknown> {
|
|
3
3
|
seed: string;
|
|
4
4
|
value: T;
|
|
5
|
-
|
|
5
|
+
addGUI(gui: Gui, min?: number, max?: number, step?: number): GuiController;
|
|
6
6
|
getValue(): T;
|
|
7
7
|
dispose(): void;
|
|
8
8
|
}
|
|
9
9
|
export interface PRNGGroupController<T = unknown> {
|
|
10
10
|
seed: string;
|
|
11
|
-
|
|
11
|
+
addGUI(gui: Gui, min?: number, max?: number, step?: number): Gui;
|
|
12
12
|
createController(index: number): PRNGController<T>;
|
|
13
13
|
getValueAt(index: number): T;
|
|
14
14
|
dispose(): void;
|
|
@@ -18,7 +18,7 @@ declare abstract class BasePRNGController<T> implements PRNGController<T> {
|
|
|
18
18
|
abstract value: T;
|
|
19
19
|
gui: GuiController;
|
|
20
20
|
constructor(seed: string);
|
|
21
|
-
abstract
|
|
21
|
+
abstract addGUI(gui: Gui, min?: number, max?: number, step?: number): GuiController;
|
|
22
22
|
abstract getValue(): T;
|
|
23
23
|
dispose(): void;
|
|
24
24
|
}
|
|
@@ -32,7 +32,7 @@ declare abstract class BasePRNGGroupController<T> implements PRNGGroupController
|
|
|
32
32
|
step?: number;
|
|
33
33
|
};
|
|
34
34
|
constructor(seed: string);
|
|
35
|
-
|
|
35
|
+
addGUI(gui: Gui, min?: number, max?: number, step?: number): Gui;
|
|
36
36
|
abstract createController(index: number): PRNGController<T>;
|
|
37
37
|
getValueAt(index: number): T;
|
|
38
38
|
dispose(): void;
|
|
@@ -41,14 +41,14 @@ export declare class BooleanController extends BasePRNGController<boolean> {
|
|
|
41
41
|
value: boolean;
|
|
42
42
|
probability: number;
|
|
43
43
|
constructor(seed: string, probability?: number);
|
|
44
|
-
|
|
44
|
+
addGUI(gui: Gui): GuiController;
|
|
45
45
|
getValue(): boolean;
|
|
46
46
|
}
|
|
47
47
|
export declare class SignController extends BasePRNGController<number> {
|
|
48
48
|
value: number;
|
|
49
49
|
probability: number;
|
|
50
50
|
constructor(seed: string, probability?: number);
|
|
51
|
-
|
|
51
|
+
addGUI(gui: Gui): GuiController;
|
|
52
52
|
getValue(): number;
|
|
53
53
|
}
|
|
54
54
|
export declare class FloatController extends BasePRNGController<number> {
|
|
@@ -56,7 +56,7 @@ export declare class FloatController extends BasePRNGController<number> {
|
|
|
56
56
|
min: number;
|
|
57
57
|
max: number;
|
|
58
58
|
constructor(seed: string, min?: number, max?: number);
|
|
59
|
-
|
|
59
|
+
addGUI(gui: Gui, min: number, max: number, step?: number): GuiController;
|
|
60
60
|
getValue(): number;
|
|
61
61
|
}
|
|
62
62
|
export declare class IntController extends BasePRNGController<number> {
|
|
@@ -64,20 +64,20 @@ export declare class IntController extends BasePRNGController<number> {
|
|
|
64
64
|
min: number;
|
|
65
65
|
max: number;
|
|
66
66
|
constructor(seed: string, min: number, max: number);
|
|
67
|
-
|
|
67
|
+
addGUI(gui: Gui, min: number, max: number, step?: number): GuiController;
|
|
68
68
|
getValue(): number;
|
|
69
69
|
}
|
|
70
70
|
export declare class HexColorController extends BasePRNGController<string> {
|
|
71
71
|
value: string;
|
|
72
72
|
constructor(seed: string);
|
|
73
|
-
|
|
73
|
+
addGUI(gui: Gui): GuiController;
|
|
74
74
|
getValue(): string;
|
|
75
75
|
}
|
|
76
76
|
export declare class ItemController<T = unknown> extends BasePRNGController<T> {
|
|
77
77
|
value: T;
|
|
78
78
|
items: T[];
|
|
79
79
|
constructor(seed: string, items: T[]);
|
|
80
|
-
|
|
80
|
+
addGUI(gui: Gui): GuiController;
|
|
81
81
|
getValue(): T;
|
|
82
82
|
}
|
|
83
83
|
export declare class ObjectPropertyController<T = unknown> extends BasePRNGController<T> {
|
|
@@ -88,7 +88,7 @@ export declare class ObjectPropertyController<T = unknown> extends BasePRNGContr
|
|
|
88
88
|
constructor(seed: string, object: {
|
|
89
89
|
[key: string]: T;
|
|
90
90
|
});
|
|
91
|
-
|
|
91
|
+
addGUI(gui: Gui): GuiController;
|
|
92
92
|
getValue(): T;
|
|
93
93
|
}
|
|
94
94
|
type WeightsItems<T> = Array<{
|
|
@@ -100,7 +100,7 @@ export declare class WeightsController<T = unknown> extends BasePRNGController<T
|
|
|
100
100
|
items: WeightsItems<T>;
|
|
101
101
|
weights: number[];
|
|
102
102
|
constructor(seed: string, items: WeightsItems<T>);
|
|
103
|
-
|
|
103
|
+
addGUI(gui: Gui): GuiController;
|
|
104
104
|
getValue(): T;
|
|
105
105
|
}
|
|
106
106
|
export declare class BooleanGroupController extends BasePRNGGroupController<boolean> {
|