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