toosoon-prng 1.5.0 → 2.0.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 +9 -163
- package/package.json +2 -6
- package/src/index.ts +1 -20
- package/src/prng.ts +21 -42
- package/src/types.ts +1 -3
- package/src/utils.ts +119 -0
- package/.prettierrc +0 -7
- package/LICENSE +0 -21
- package/lib/controllers.d.ts +0 -150
- package/lib/controllers.js +0 -363
- package/lib/index.d.ts +0 -3
- package/lib/index.js +0 -2
- package/lib/prng.d.ts +0 -119
- package/lib/prng.js +0 -192
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/lib/types.d.ts +0 -8
- package/lib/types.js +0 -8
- package/lib/utils.d.ts +0 -45
- package/lib/utils.js +0 -115
- package/src/controllers.ts +0 -409
package/lib/controllers.js
DELETED
|
@@ -1,363 +0,0 @@
|
|
|
1
|
-
var __extends = (this && this.__extends) || (function () {
|
|
2
|
-
var extendStatics = function (d, b) {
|
|
3
|
-
extendStatics = Object.setPrototypeOf ||
|
|
4
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
-
return extendStatics(d, b);
|
|
7
|
-
};
|
|
8
|
-
return function (d, b) {
|
|
9
|
-
if (typeof b !== "function" && b !== null)
|
|
10
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
-
extendStatics(d, b);
|
|
12
|
-
function __() { this.constructor = d; }
|
|
13
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
-
};
|
|
15
|
-
})();
|
|
16
|
-
import prng from './prng';
|
|
17
|
-
// *********************
|
|
18
|
-
// Abstract controllers
|
|
19
|
-
// *********************
|
|
20
|
-
var BasePRNGController = /** @class */ (function () {
|
|
21
|
-
function BasePRNGController(seed) {
|
|
22
|
-
this.seed = seed;
|
|
23
|
-
prng.addController(this);
|
|
24
|
-
}
|
|
25
|
-
BasePRNGController.prototype.dispose = function () {
|
|
26
|
-
var _a;
|
|
27
|
-
prng.removeController(this);
|
|
28
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
29
|
-
};
|
|
30
|
-
return BasePRNGController;
|
|
31
|
-
}());
|
|
32
|
-
var BasePRNGGroupController = /** @class */ (function () {
|
|
33
|
-
function BasePRNGGroupController(seed) {
|
|
34
|
-
this.controllers = [];
|
|
35
|
-
this.seed = seed;
|
|
36
|
-
}
|
|
37
|
-
BasePRNGGroupController.prototype.addGUI = function (gui, min, max, step) {
|
|
38
|
-
this.gui = gui.addFolder(this.seed).close();
|
|
39
|
-
this.guiArgs = { min: min, max: max, step: step };
|
|
40
|
-
return this.gui;
|
|
41
|
-
};
|
|
42
|
-
BasePRNGGroupController.prototype.getValueAt = function (index) {
|
|
43
|
-
var controller = this.controllers[index];
|
|
44
|
-
if (!controller) {
|
|
45
|
-
controller = this.createController(index);
|
|
46
|
-
if (this.gui) {
|
|
47
|
-
controller
|
|
48
|
-
.addGUI(this.gui, this.guiArgs.min, this.guiArgs.max, this.guiArgs.step)
|
|
49
|
-
.name("".concat(this.seed, "-").concat(index));
|
|
50
|
-
}
|
|
51
|
-
this.controllers[index] = controller;
|
|
52
|
-
}
|
|
53
|
-
return controller.value;
|
|
54
|
-
};
|
|
55
|
-
BasePRNGGroupController.prototype.dispose = function () {
|
|
56
|
-
var _a;
|
|
57
|
-
this.controllers.forEach(function (controller) { return controller.dispose(); });
|
|
58
|
-
this.controllers = [];
|
|
59
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
60
|
-
};
|
|
61
|
-
return BasePRNGGroupController;
|
|
62
|
-
}());
|
|
63
|
-
// *********************
|
|
64
|
-
// Controllers
|
|
65
|
-
// *********************
|
|
66
|
-
var BooleanController = /** @class */ (function (_super) {
|
|
67
|
-
__extends(BooleanController, _super);
|
|
68
|
-
function BooleanController(seed, probability) {
|
|
69
|
-
if (probability === void 0) { probability = 0.5; }
|
|
70
|
-
var _this = _super.call(this, seed) || this;
|
|
71
|
-
_this.probability = probability;
|
|
72
|
-
_this.value = _this.getValue();
|
|
73
|
-
return _this;
|
|
74
|
-
}
|
|
75
|
-
BooleanController.prototype.addGUI = function (gui) {
|
|
76
|
-
this.gui = gui.add(this, 'value').name(this.seed);
|
|
77
|
-
return this.gui;
|
|
78
|
-
};
|
|
79
|
-
BooleanController.prototype.getValue = function () {
|
|
80
|
-
var _a;
|
|
81
|
-
this.value = prng.randomBoolean(this.seed, this.probability);
|
|
82
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.updateDisplay();
|
|
83
|
-
return this.value;
|
|
84
|
-
};
|
|
85
|
-
return BooleanController;
|
|
86
|
-
}(BasePRNGController));
|
|
87
|
-
export { BooleanController };
|
|
88
|
-
var SignController = /** @class */ (function (_super) {
|
|
89
|
-
__extends(SignController, _super);
|
|
90
|
-
function SignController(seed, probability) {
|
|
91
|
-
if (probability === void 0) { probability = 0.5; }
|
|
92
|
-
var _this = _super.call(this, seed) || this;
|
|
93
|
-
_this.probability = probability;
|
|
94
|
-
_this.value = _this.getValue();
|
|
95
|
-
return _this;
|
|
96
|
-
}
|
|
97
|
-
SignController.prototype.addGUI = function (gui) {
|
|
98
|
-
this.gui = gui.add(this, 'value', [-1, 1]).name(this.seed);
|
|
99
|
-
return this.gui;
|
|
100
|
-
};
|
|
101
|
-
SignController.prototype.getValue = function () {
|
|
102
|
-
var _a;
|
|
103
|
-
this.value = prng.randomSign(this.seed, this.probability);
|
|
104
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.updateDisplay();
|
|
105
|
-
return this.value;
|
|
106
|
-
};
|
|
107
|
-
return SignController;
|
|
108
|
-
}(BasePRNGController));
|
|
109
|
-
export { SignController };
|
|
110
|
-
var FloatController = /** @class */ (function (_super) {
|
|
111
|
-
__extends(FloatController, _super);
|
|
112
|
-
function FloatController(seed, min, max) {
|
|
113
|
-
if (min === void 0) { min = 0; }
|
|
114
|
-
if (max === void 0) { max = 1; }
|
|
115
|
-
var _this = _super.call(this, seed) || this;
|
|
116
|
-
_this.min = min;
|
|
117
|
-
_this.max = max;
|
|
118
|
-
_this.value = _this.getValue();
|
|
119
|
-
return _this;
|
|
120
|
-
}
|
|
121
|
-
FloatController.prototype.addGUI = function (gui, min, max, step) {
|
|
122
|
-
if (step === void 0) { step = 0.01; }
|
|
123
|
-
this.gui = gui.add(this, 'value', min, max, step).name(this.seed);
|
|
124
|
-
return this.gui;
|
|
125
|
-
};
|
|
126
|
-
FloatController.prototype.getValue = function () {
|
|
127
|
-
var _a;
|
|
128
|
-
this.value = prng.randomFloat(this.seed, this.min, this.max);
|
|
129
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.updateDisplay();
|
|
130
|
-
return this.value;
|
|
131
|
-
};
|
|
132
|
-
return FloatController;
|
|
133
|
-
}(BasePRNGController));
|
|
134
|
-
export { FloatController };
|
|
135
|
-
var IntController = /** @class */ (function (_super) {
|
|
136
|
-
__extends(IntController, _super);
|
|
137
|
-
function IntController(seed, min, max) {
|
|
138
|
-
var _this = _super.call(this, seed) || this;
|
|
139
|
-
_this.min = min;
|
|
140
|
-
_this.max = max;
|
|
141
|
-
_this.value = prng.randomInt(_this.seed, min, max);
|
|
142
|
-
return _this;
|
|
143
|
-
}
|
|
144
|
-
IntController.prototype.addGUI = function (gui, min, max, step) {
|
|
145
|
-
if (step === void 0) { step = 1; }
|
|
146
|
-
this.gui = gui.add(this, 'value', min, max, step).name(this.seed);
|
|
147
|
-
return this.gui;
|
|
148
|
-
};
|
|
149
|
-
IntController.prototype.getValue = function () {
|
|
150
|
-
var _a;
|
|
151
|
-
this.value = prng.randomInt(this.seed, this.min, this.max);
|
|
152
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.updateDisplay();
|
|
153
|
-
return this.value;
|
|
154
|
-
};
|
|
155
|
-
return IntController;
|
|
156
|
-
}(BasePRNGController));
|
|
157
|
-
export { IntController };
|
|
158
|
-
var HexColorController = /** @class */ (function (_super) {
|
|
159
|
-
__extends(HexColorController, _super);
|
|
160
|
-
function HexColorController(seed) {
|
|
161
|
-
var _this = _super.call(this, seed) || this;
|
|
162
|
-
_this.value = _this.getValue();
|
|
163
|
-
return _this;
|
|
164
|
-
}
|
|
165
|
-
HexColorController.prototype.addGUI = function (gui) {
|
|
166
|
-
this.gui = gui.addColor(this, 'value').name(this.seed);
|
|
167
|
-
return this.gui;
|
|
168
|
-
};
|
|
169
|
-
HexColorController.prototype.getValue = function () {
|
|
170
|
-
var _a;
|
|
171
|
-
this.value = prng.randomHexColor(this.seed);
|
|
172
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.updateDisplay();
|
|
173
|
-
return this.value;
|
|
174
|
-
};
|
|
175
|
-
return HexColorController;
|
|
176
|
-
}(BasePRNGController));
|
|
177
|
-
export { HexColorController };
|
|
178
|
-
var ItemController = /** @class */ (function (_super) {
|
|
179
|
-
__extends(ItemController, _super);
|
|
180
|
-
function ItemController(seed, items) {
|
|
181
|
-
var _this = _super.call(this, seed) || this;
|
|
182
|
-
_this.items = items;
|
|
183
|
-
_this.value = _this.getValue();
|
|
184
|
-
return _this;
|
|
185
|
-
}
|
|
186
|
-
ItemController.prototype.addGUI = function (gui) {
|
|
187
|
-
this.gui = gui.add(this, 'value', this.items).name(this.seed);
|
|
188
|
-
return this.gui;
|
|
189
|
-
};
|
|
190
|
-
ItemController.prototype.getValue = function () {
|
|
191
|
-
var _a;
|
|
192
|
-
this.value = prng.randomItem(this.seed, this.items);
|
|
193
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.updateDisplay();
|
|
194
|
-
return this.value;
|
|
195
|
-
};
|
|
196
|
-
return ItemController;
|
|
197
|
-
}(BasePRNGController));
|
|
198
|
-
export { ItemController };
|
|
199
|
-
var ObjectPropertyController = /** @class */ (function (_super) {
|
|
200
|
-
__extends(ObjectPropertyController, _super);
|
|
201
|
-
function ObjectPropertyController(seed, object) {
|
|
202
|
-
var _this = _super.call(this, seed) || this;
|
|
203
|
-
_this.object = object;
|
|
204
|
-
_this.value = _this.getValue();
|
|
205
|
-
return _this;
|
|
206
|
-
}
|
|
207
|
-
ObjectPropertyController.prototype.addGUI = function (gui) {
|
|
208
|
-
this.gui = gui.add(this, 'value', this.object).name(this.seed);
|
|
209
|
-
return this.gui;
|
|
210
|
-
};
|
|
211
|
-
ObjectPropertyController.prototype.getValue = function () {
|
|
212
|
-
var _a;
|
|
213
|
-
this.value = prng.randomObjectProperty(this.seed, this.object);
|
|
214
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.updateDisplay();
|
|
215
|
-
return this.value;
|
|
216
|
-
};
|
|
217
|
-
return ObjectPropertyController;
|
|
218
|
-
}(BasePRNGController));
|
|
219
|
-
export { ObjectPropertyController };
|
|
220
|
-
var WeightsController = /** @class */ (function (_super) {
|
|
221
|
-
__extends(WeightsController, _super);
|
|
222
|
-
function WeightsController(seed, items) {
|
|
223
|
-
var _this = _super.call(this, seed) || this;
|
|
224
|
-
_this.items = items;
|
|
225
|
-
_this.weights = _this.items.map(function (item) { return item.weight; });
|
|
226
|
-
_this.value = _this.getValue();
|
|
227
|
-
return _this;
|
|
228
|
-
}
|
|
229
|
-
WeightsController.prototype.addGUI = function (gui) {
|
|
230
|
-
this.gui = gui
|
|
231
|
-
.add(this, 'value', this.items.map(function (item) { return item.value; }))
|
|
232
|
-
.name(this.seed);
|
|
233
|
-
return this.gui;
|
|
234
|
-
};
|
|
235
|
-
WeightsController.prototype.getValue = function () {
|
|
236
|
-
var _a;
|
|
237
|
-
var index = prng.randomIndex(this.seed, this.weights);
|
|
238
|
-
this.value = this.items[index].value;
|
|
239
|
-
(_a = this.gui) === null || _a === void 0 ? void 0 : _a.updateDisplay();
|
|
240
|
-
return this.value;
|
|
241
|
-
};
|
|
242
|
-
return WeightsController;
|
|
243
|
-
}(BasePRNGController));
|
|
244
|
-
export { WeightsController };
|
|
245
|
-
// *********************
|
|
246
|
-
// Group Controllers
|
|
247
|
-
// *********************
|
|
248
|
-
var BooleanGroupController = /** @class */ (function (_super) {
|
|
249
|
-
__extends(BooleanGroupController, _super);
|
|
250
|
-
function BooleanGroupController(seed, probability) {
|
|
251
|
-
var _this = _super.call(this, seed) || this;
|
|
252
|
-
_this.controllers = [];
|
|
253
|
-
_this.probability = probability;
|
|
254
|
-
return _this;
|
|
255
|
-
}
|
|
256
|
-
BooleanGroupController.prototype.createController = function (index) {
|
|
257
|
-
return new BooleanController("".concat(this.seed, "-").concat(index), this.probability);
|
|
258
|
-
};
|
|
259
|
-
return BooleanGroupController;
|
|
260
|
-
}(BasePRNGGroupController));
|
|
261
|
-
export { BooleanGroupController };
|
|
262
|
-
var SignGroupController = /** @class */ (function (_super) {
|
|
263
|
-
__extends(SignGroupController, _super);
|
|
264
|
-
function SignGroupController(seed, probability) {
|
|
265
|
-
var _this = _super.call(this, seed) || this;
|
|
266
|
-
_this.controllers = [];
|
|
267
|
-
_this.probability = probability;
|
|
268
|
-
return _this;
|
|
269
|
-
}
|
|
270
|
-
SignGroupController.prototype.createController = function (index) {
|
|
271
|
-
return new SignController("".concat(this.seed, "-").concat(index), this.probability);
|
|
272
|
-
};
|
|
273
|
-
return SignGroupController;
|
|
274
|
-
}(BasePRNGGroupController));
|
|
275
|
-
export { SignGroupController };
|
|
276
|
-
var FloatGroupController = /** @class */ (function (_super) {
|
|
277
|
-
__extends(FloatGroupController, _super);
|
|
278
|
-
function FloatGroupController(seed, min, max) {
|
|
279
|
-
var _this = _super.call(this, seed) || this;
|
|
280
|
-
_this.controllers = [];
|
|
281
|
-
_this.min = min;
|
|
282
|
-
_this.max = max;
|
|
283
|
-
return _this;
|
|
284
|
-
}
|
|
285
|
-
FloatGroupController.prototype.createController = function (index) {
|
|
286
|
-
return new FloatController("".concat(this.seed, "-").concat(index), this.min, this.max);
|
|
287
|
-
};
|
|
288
|
-
return FloatGroupController;
|
|
289
|
-
}(BasePRNGGroupController));
|
|
290
|
-
export { FloatGroupController };
|
|
291
|
-
var IntGroupController = /** @class */ (function (_super) {
|
|
292
|
-
__extends(IntGroupController, _super);
|
|
293
|
-
function IntGroupController(seed, min, max) {
|
|
294
|
-
var _this = _super.call(this, seed) || this;
|
|
295
|
-
_this.controllers = [];
|
|
296
|
-
_this.min = min;
|
|
297
|
-
_this.max = max;
|
|
298
|
-
return _this;
|
|
299
|
-
}
|
|
300
|
-
IntGroupController.prototype.createController = function (index) {
|
|
301
|
-
return new IntController("".concat(this.seed, "-").concat(index), this.min, this.max);
|
|
302
|
-
};
|
|
303
|
-
return IntGroupController;
|
|
304
|
-
}(BasePRNGGroupController));
|
|
305
|
-
export { IntGroupController };
|
|
306
|
-
var HexColorGroupController = /** @class */ (function (_super) {
|
|
307
|
-
__extends(HexColorGroupController, _super);
|
|
308
|
-
function HexColorGroupController() {
|
|
309
|
-
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
310
|
-
_this.controllers = [];
|
|
311
|
-
return _this;
|
|
312
|
-
}
|
|
313
|
-
// constructor(seed: string) {
|
|
314
|
-
// super(seed);
|
|
315
|
-
// }
|
|
316
|
-
HexColorGroupController.prototype.createController = function (index) {
|
|
317
|
-
return new HexColorController("".concat(this.seed, "-").concat(index));
|
|
318
|
-
};
|
|
319
|
-
return HexColorGroupController;
|
|
320
|
-
}(BasePRNGGroupController));
|
|
321
|
-
export { HexColorGroupController };
|
|
322
|
-
var ItemGroupController = /** @class */ (function (_super) {
|
|
323
|
-
__extends(ItemGroupController, _super);
|
|
324
|
-
function ItemGroupController(seed, items) {
|
|
325
|
-
var _this = _super.call(this, seed) || this;
|
|
326
|
-
_this.controllers = [];
|
|
327
|
-
_this.items = items;
|
|
328
|
-
return _this;
|
|
329
|
-
}
|
|
330
|
-
ItemGroupController.prototype.createController = function (index) {
|
|
331
|
-
return new ItemController("".concat(this.seed, "-").concat(index), this.items);
|
|
332
|
-
};
|
|
333
|
-
return ItemGroupController;
|
|
334
|
-
}(BasePRNGGroupController));
|
|
335
|
-
export { ItemGroupController };
|
|
336
|
-
var ObjectPropertyGroupController = /** @class */ (function (_super) {
|
|
337
|
-
__extends(ObjectPropertyGroupController, _super);
|
|
338
|
-
function ObjectPropertyGroupController(seed, object) {
|
|
339
|
-
var _this = _super.call(this, seed) || this;
|
|
340
|
-
_this.controllers = [];
|
|
341
|
-
_this.object = object;
|
|
342
|
-
return _this;
|
|
343
|
-
}
|
|
344
|
-
ObjectPropertyGroupController.prototype.createController = function (index) {
|
|
345
|
-
return new ObjectPropertyController("".concat(this.seed, "-").concat(index), this.object);
|
|
346
|
-
};
|
|
347
|
-
return ObjectPropertyGroupController;
|
|
348
|
-
}(BasePRNGGroupController));
|
|
349
|
-
export { ObjectPropertyGroupController };
|
|
350
|
-
var WeightsGroupController = /** @class */ (function (_super) {
|
|
351
|
-
__extends(WeightsGroupController, _super);
|
|
352
|
-
function WeightsGroupController(seed, items) {
|
|
353
|
-
var _this = _super.call(this, seed) || this;
|
|
354
|
-
_this.controllers = [];
|
|
355
|
-
_this.items = items;
|
|
356
|
-
return _this;
|
|
357
|
-
}
|
|
358
|
-
WeightsGroupController.prototype.createController = function (index) {
|
|
359
|
-
return new WeightsController("".concat(this.seed, "-").concat(index), this.items);
|
|
360
|
-
};
|
|
361
|
-
return WeightsGroupController;
|
|
362
|
-
}(BasePRNGGroupController));
|
|
363
|
-
export { WeightsGroupController };
|
package/lib/index.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export { BooleanController, SignController, IntController, FloatController, HexColorController, ItemController, ObjectPropertyController, WeightsController, BooleanGroupController, SignGroupController, IntGroupController, FloatGroupController, HexColorGroupController, ItemGroupController, ObjectPropertyGroupController, WeightsGroupController } from './controllers';
|
|
2
|
-
export { default } from './prng';
|
|
3
|
-
export type * from './types';
|
package/lib/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export { BooleanController, SignController, IntController, FloatController, HexColorController, ItemController, ObjectPropertyController, WeightsController, BooleanGroupController, SignGroupController, IntGroupController, FloatGroupController, HexColorGroupController, ItemGroupController, ObjectPropertyGroupController, WeightsGroupController } from './controllers';
|
|
2
|
-
export { default } from './prng';
|
package/lib/prng.d.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { PRNGController } from './controllers';
|
|
2
|
-
import { Algorithm } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* Utility class for generating pseudo-random values
|
|
5
|
-
*
|
|
6
|
-
* @class PRNG
|
|
7
|
-
*/
|
|
8
|
-
declare class PRNG {
|
|
9
|
-
seed: string;
|
|
10
|
-
algorithm: (...args: number[]) => number;
|
|
11
|
-
controllers: PRNGController[];
|
|
12
|
-
/**
|
|
13
|
-
* Add a controller to this PRNG
|
|
14
|
-
*
|
|
15
|
-
* @param {PRNGController} controller
|
|
16
|
-
*/
|
|
17
|
-
addController(controller: PRNGController): void;
|
|
18
|
-
/**
|
|
19
|
-
* Remove a controller from this PRNG
|
|
20
|
-
*
|
|
21
|
-
* @param {PRNGController} controller
|
|
22
|
-
*/
|
|
23
|
-
removeController(controller: PRNGController): void;
|
|
24
|
-
/**
|
|
25
|
-
* Set this PRNG seed
|
|
26
|
-
*
|
|
27
|
-
* @param {string} seed
|
|
28
|
-
*/
|
|
29
|
-
setSeed(seed: string): void;
|
|
30
|
-
/**
|
|
31
|
-
* Set this PRNG algorithm for generating pseudo-random values
|
|
32
|
-
*
|
|
33
|
-
* @param {Algorithm} algorithm Algorithm name
|
|
34
|
-
*/
|
|
35
|
-
setAlgorithm(algorithm: Algorithm): void;
|
|
36
|
-
/**
|
|
37
|
-
* Generate a pseudo-random number in the interval [0, 1]
|
|
38
|
-
* PRNG equivalent of `Math.random()`
|
|
39
|
-
*
|
|
40
|
-
* @param {string} seed
|
|
41
|
-
* @returns {number}
|
|
42
|
-
*/
|
|
43
|
-
random(seed: string): number;
|
|
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
|
-
randomBoolean(seed: string, probability?: number): boolean;
|
|
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
|
-
randomSign(seed: string, probability?: number): number;
|
|
60
|
-
/**
|
|
61
|
-
* Generate a pseudo-random floating-point number within a specified range
|
|
62
|
-
*
|
|
63
|
-
* @param {string} seed
|
|
64
|
-
* @param {number} [min=0] Minimum boundary
|
|
65
|
-
* @param {number} [max=1] Maximum boundary
|
|
66
|
-
* @param {number} [precision=2] Number of digits after the decimal point
|
|
67
|
-
* @returns {number} Generated float
|
|
68
|
-
*/
|
|
69
|
-
randomFloat(seed: string, min?: number, max?: number, precision?: number): number;
|
|
70
|
-
/**
|
|
71
|
-
* Generate a pseudo-random integer number within a specified range
|
|
72
|
-
*
|
|
73
|
-
* @param {string} seed
|
|
74
|
-
* @param {number} min Minimum boundary
|
|
75
|
-
* @param {number} max Maximum boundary
|
|
76
|
-
* @returns {number} Generated integer
|
|
77
|
-
*/
|
|
78
|
-
randomInt(seed: string, min: number, max: number): number;
|
|
79
|
-
/**
|
|
80
|
-
* Generate a pseudo-random hexadecimal color
|
|
81
|
-
*
|
|
82
|
-
* @param {string} seed
|
|
83
|
-
* @returns {string} Generated hexadecimal color
|
|
84
|
-
*/
|
|
85
|
-
randomHexColor(seed: string): string;
|
|
86
|
-
/**
|
|
87
|
-
* Pick a pseudo-random item from a given array
|
|
88
|
-
*
|
|
89
|
-
* @param {string} seed
|
|
90
|
-
* @param {T[]} array Array to pick the item from
|
|
91
|
-
* @returns {T|undefined} Random item picked
|
|
92
|
-
*/
|
|
93
|
-
randomItem<T = unknown>(seed: string, array: T[]): T | undefined;
|
|
94
|
-
/**
|
|
95
|
-
* Pick a pseudo-random property value from a given object
|
|
96
|
-
*
|
|
97
|
-
* @param {string} seed
|
|
98
|
-
* @param {object} object Object to pick the property from
|
|
99
|
-
* @returns {T|undefined} Random item picked
|
|
100
|
-
*/
|
|
101
|
-
randomObjectProperty<T = unknown>(seed: string, object: Record<string, T>): T | undefined;
|
|
102
|
-
/**
|
|
103
|
-
* Select a pseudo-random index from an array of weighted items
|
|
104
|
-
*
|
|
105
|
-
* @param {string} seed
|
|
106
|
-
* @param {number[]} weights Array of weights
|
|
107
|
-
* @returns {number} Random index based on weights
|
|
108
|
-
*/
|
|
109
|
-
randomIndex(seed: string, weights: number[]): number;
|
|
110
|
-
/**
|
|
111
|
-
* Get the PRNG algorithm function by its name
|
|
112
|
-
*
|
|
113
|
-
* @param {Algorithm} algorithm Algorithm name
|
|
114
|
-
* @returns {Function} PRNG algorithm function
|
|
115
|
-
*/
|
|
116
|
-
private getAlgorithm;
|
|
117
|
-
}
|
|
118
|
-
declare const prng: PRNG;
|
|
119
|
-
export default prng;
|
package/lib/prng.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import { jsf32, mulberry32, sfc32, splitmix32, xoshiro128ss, random } from 'toosoon-utils/prng';
|
|
2
|
-
import { Algorithm } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* Utility class for generating pseudo-random values
|
|
5
|
-
*
|
|
6
|
-
* @class PRNG
|
|
7
|
-
*/
|
|
8
|
-
var PRNG = /** @class */ (function () {
|
|
9
|
-
function PRNG() {
|
|
10
|
-
this.seed = '';
|
|
11
|
-
this.algorithm = splitmix32;
|
|
12
|
-
this.controllers = [];
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Add a controller to this PRNG
|
|
16
|
-
*
|
|
17
|
-
* @param {PRNGController} controller
|
|
18
|
-
*/
|
|
19
|
-
PRNG.prototype.addController = function (controller) {
|
|
20
|
-
this.controllers.push(controller);
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Remove a controller from this PRNG
|
|
24
|
-
*
|
|
25
|
-
* @param {PRNGController} controller
|
|
26
|
-
*/
|
|
27
|
-
PRNG.prototype.removeController = function (controller) {
|
|
28
|
-
var index = this.controllers.indexOf(controller);
|
|
29
|
-
this.controllers.splice(index, 1);
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* Set this PRNG seed
|
|
33
|
-
*
|
|
34
|
-
* @param {string} seed
|
|
35
|
-
*/
|
|
36
|
-
PRNG.prototype.setSeed = function (seed) {
|
|
37
|
-
if (this.seed === seed)
|
|
38
|
-
return;
|
|
39
|
-
this.seed = seed;
|
|
40
|
-
this.controllers.forEach(function (controller) { return controller.getValue(); });
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* Set this PRNG algorithm for generating pseudo-random values
|
|
44
|
-
*
|
|
45
|
-
* @param {Algorithm} algorithm Algorithm name
|
|
46
|
-
*/
|
|
47
|
-
PRNG.prototype.setAlgorithm = function (algorithm) {
|
|
48
|
-
this.algorithm = this.getAlgorithm(algorithm);
|
|
49
|
-
this.controllers.forEach(function (controller) { return controller.getValue(); });
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* Generate a pseudo-random number in the interval [0, 1]
|
|
53
|
-
* PRNG equivalent of `Math.random()`
|
|
54
|
-
*
|
|
55
|
-
* @param {string} seed
|
|
56
|
-
* @returns {number}
|
|
57
|
-
*/
|
|
58
|
-
PRNG.prototype.random = function (seed) {
|
|
59
|
-
return random({ seed: this.seed + seed, algorithm: this.algorithm });
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* Generate a pseudo-random boolean (true or false)
|
|
63
|
-
*
|
|
64
|
-
* @param {string} seed
|
|
65
|
-
* @param {number} [probability=0.5] Probability to get `true`
|
|
66
|
-
* @returns {boolean} Either `true` or `false`
|
|
67
|
-
*/
|
|
68
|
-
PRNG.prototype.randomBoolean = function (seed, probability) {
|
|
69
|
-
if (probability === void 0) { probability = 0.5; }
|
|
70
|
-
return this.random(seed) < probability;
|
|
71
|
-
};
|
|
72
|
-
/**
|
|
73
|
-
* Generate a pseudo-random sign (1 or -1)
|
|
74
|
-
*
|
|
75
|
-
* @param {string} seed
|
|
76
|
-
* @param {number} [probability=0.5] Probability to get 1
|
|
77
|
-
* @returns {number} Either 1 or -1
|
|
78
|
-
*/
|
|
79
|
-
PRNG.prototype.randomSign = function (seed, probability) {
|
|
80
|
-
if (probability === void 0) { probability = 0.5; }
|
|
81
|
-
return this.randomBoolean(seed, probability) ? 1 : -1;
|
|
82
|
-
};
|
|
83
|
-
/**
|
|
84
|
-
* Generate a pseudo-random floating-point number within a specified range
|
|
85
|
-
*
|
|
86
|
-
* @param {string} seed
|
|
87
|
-
* @param {number} [min=0] Minimum boundary
|
|
88
|
-
* @param {number} [max=1] Maximum boundary
|
|
89
|
-
* @param {number} [precision=2] Number of digits after the decimal point
|
|
90
|
-
* @returns {number} Generated float
|
|
91
|
-
*/
|
|
92
|
-
PRNG.prototype.randomFloat = function (seed, min, max, precision) {
|
|
93
|
-
if (min === void 0) { min = 0; }
|
|
94
|
-
if (max === void 0) { max = 1; }
|
|
95
|
-
if (precision === void 0) { precision = 2; }
|
|
96
|
-
return parseFloat(Math.min(min + this.random(seed) * (max - min), max).toFixed(precision));
|
|
97
|
-
};
|
|
98
|
-
/**
|
|
99
|
-
* Generate a pseudo-random integer number within a specified range
|
|
100
|
-
*
|
|
101
|
-
* @param {string} seed
|
|
102
|
-
* @param {number} min Minimum boundary
|
|
103
|
-
* @param {number} max Maximum boundary
|
|
104
|
-
* @returns {number} Generated integer
|
|
105
|
-
*/
|
|
106
|
-
PRNG.prototype.randomInt = function (seed, min, max) {
|
|
107
|
-
return Math.floor(this.random(seed) * (max - min + 1) + min);
|
|
108
|
-
};
|
|
109
|
-
/**
|
|
110
|
-
* Generate a pseudo-random hexadecimal color
|
|
111
|
-
*
|
|
112
|
-
* @param {string} seed
|
|
113
|
-
* @returns {string} Generated hexadecimal color
|
|
114
|
-
*/
|
|
115
|
-
PRNG.prototype.randomHexColor = function (seed) {
|
|
116
|
-
return '#' + ('00000' + ((this.random(seed) * (1 << 24)) | 0).toString(16)).slice(-6);
|
|
117
|
-
};
|
|
118
|
-
/**
|
|
119
|
-
* Pick a pseudo-random item from a given array
|
|
120
|
-
*
|
|
121
|
-
* @param {string} seed
|
|
122
|
-
* @param {T[]} array Array to pick the item from
|
|
123
|
-
* @returns {T|undefined} Random item picked
|
|
124
|
-
*/
|
|
125
|
-
PRNG.prototype.randomItem = function (seed, array) {
|
|
126
|
-
if (array.length === 0)
|
|
127
|
-
return undefined;
|
|
128
|
-
return array[this.randomInt(seed, 0, array.length - 1)];
|
|
129
|
-
};
|
|
130
|
-
/**
|
|
131
|
-
* Pick a pseudo-random property value from a given object
|
|
132
|
-
*
|
|
133
|
-
* @param {string} seed
|
|
134
|
-
* @param {object} object Object to pick the property from
|
|
135
|
-
* @returns {T|undefined} Random item picked
|
|
136
|
-
*/
|
|
137
|
-
PRNG.prototype.randomObjectProperty = function (seed, object) {
|
|
138
|
-
var keys = Object.keys(object);
|
|
139
|
-
var key = this.randomItem(seed, keys);
|
|
140
|
-
if (key && object.hasOwnProperty(key)) {
|
|
141
|
-
return object[key];
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
/**
|
|
145
|
-
* Select a pseudo-random index from an array of weighted items
|
|
146
|
-
*
|
|
147
|
-
* @param {string} seed
|
|
148
|
-
* @param {number[]} weights Array of weights
|
|
149
|
-
* @returns {number} Random index based on weights
|
|
150
|
-
*/
|
|
151
|
-
PRNG.prototype.randomIndex = function (seed, weights) {
|
|
152
|
-
if (weights.length === 0)
|
|
153
|
-
return -1;
|
|
154
|
-
var totalWeight = 0;
|
|
155
|
-
for (var _i = 0, weights_1 = weights; _i < weights_1.length; _i++) {
|
|
156
|
-
var weight_1 = weights_1[_i];
|
|
157
|
-
totalWeight += weight_1;
|
|
158
|
-
}
|
|
159
|
-
if (totalWeight <= 0)
|
|
160
|
-
console.warn('PRNG.randomIndex()', 'Weights must sum to > 0', totalWeight);
|
|
161
|
-
var weight = this.random(seed) * totalWeight;
|
|
162
|
-
for (var i = 0; i < weights.length; i++) {
|
|
163
|
-
if (weight < weights[i])
|
|
164
|
-
return i;
|
|
165
|
-
weight -= weights[i];
|
|
166
|
-
}
|
|
167
|
-
return 0;
|
|
168
|
-
};
|
|
169
|
-
/**
|
|
170
|
-
* Get the PRNG algorithm function by its name
|
|
171
|
-
*
|
|
172
|
-
* @param {Algorithm} algorithm Algorithm name
|
|
173
|
-
* @returns {Function} PRNG algorithm function
|
|
174
|
-
*/
|
|
175
|
-
PRNG.prototype.getAlgorithm = function (algorithm) {
|
|
176
|
-
switch (algorithm) {
|
|
177
|
-
case Algorithm.splitmix32:
|
|
178
|
-
return splitmix32;
|
|
179
|
-
case Algorithm.jsf32:
|
|
180
|
-
return jsf32;
|
|
181
|
-
case Algorithm.mulberry32:
|
|
182
|
-
return mulberry32;
|
|
183
|
-
case Algorithm.sfc32:
|
|
184
|
-
return sfc32;
|
|
185
|
-
case Algorithm.xoshiro128ss:
|
|
186
|
-
return xoshiro128ss;
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
return PRNG;
|
|
190
|
-
}());
|
|
191
|
-
var prng = new PRNG();
|
|
192
|
-
export default prng;
|