toosoon-prng-controllers
v5.0.0
Published
This project provides PRNG functions with a set of controllers for generating pseudo-random values using a seed-based approach
Maintainers
Readme
TOOSOON Pseudo-Random Number Generator (PRNG) — Controllers
This project provides PRNG functions with a set of controllers for generating pseudo-random values using a seed-based approach.
It adds additionnal features to the main library: toosoon-prng.
Installation
Yarn:
$ yarn add toosoon-prng-controllersNPM:
$ npm install toosoon-prng-controllersUsage
import { IntController, IntGroupController } from 'toosoon-prng-controllers';
const config = {
count: new IntController('count', 0, 10),
counts: new IntGroupController('counts', 5, 10)
};
config.count.getValue(); // Pseudo-random integer in the interval [0, 10)
for (let i = 0; i < 5; i++) {
config.counts.getValueAt(i); // Pseudo-random integers in the interval [5, 10)
}Controllers
PRNGController
Utility abstract class for generating pseudo-random values.
- new PRNGController<T>(seed)
- .seed:
string - .value:
T - .getValue():
T - .dispose():
void
- .seed:
Properties
.seed
Seed string used for pseudo-random generations.
PRNGController.seed: string;.value
Pseudo-random value generated by the controller.
PRNGController<T>.value: T;Methods
.getValue()
Generate the controller pseudo-random value.
PRNGController<T>.getValue(): T;.dispose()
Dispose the controller.
PRNGController.dispose(): void;BooleanController
Utility class extending PRNGController for generating pseudo-random boolean values.
- new BooleanController(seed, probability?)
- .value:
boolean - .probability:
number
- .value:
Constructor
| Parameter | Type | Default | Description |
| ------------- | ---------| ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| [probability] | number | 0.5 | Probability to get true. |
Properties
.value
Pseudo-random boolean value generated by the controller.
BooleanController.value: boolean;.probability
Probability to get true.
BooleanController.probability: number;SignController
Utility class extending PRNGController for generating pseudo-random sign values (-1 or 1).
- new SignController(seed, probability?)
- .value:
number - .probability:
number
- .value:
Constructor
| Parameter | Type | Default | Description |
| ------------- | ---------| ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| [probability] | number | 0.5 | Probability to get 1. |
Properties
.value
Pseudo-random sign value generated by the controller.
SignController.value: number;.probability
Probability to get 1.
SignController.probability: number;FloatController
Utility class extending PRNGController for generating pseudo-random floating-point numbers within a specified range.
Constructor
| Parameter | Type | Default | Description |
| --------- | ------------------ | ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| [min] | number | 0 | Minimum boundary. |
| [max] | number | 1 | Maximum boundary. |
Properties
.value
Pseudo-random floating-point number generated by the controller.
FloatController.value: number;.min
Minimum boundary.
FloatController.min: number;.max
Maximum boundary.
FloatController.max: number;IntController
Utility class extending PRNGController for generating pseudo-random integer numbers within a specified range.
Constructor
| Parameter | Type | Default | Description |
| --------- | ------------------ | ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| min | number | | Minimum boundary. |
| max | number | | Maximum boundary. |
Properties
.value
Pseudo-random integer number generated by the controller.
IntController.value: number;.min
Minimum boundary.
IntController.min: number;.max
Maximum boundary.
IntController.max: number;HexColorController
Utility class extending PRNGController for generating pseudo-random hexadecimal color strings.
Constructor
| Parameter | Type | Default | Description |
| --------- | -------- | ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
Properties
.value
Pseudo-random hexadecimal color string generated by the controller.
HexColorController.value: string;ItemController
Utility class extending PRNGController for pseudo-randomly picking an item from an array.
Constructor
| Parameter | Type | Default | Description |
| --------- | ------ | ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| items | T[] | | Array of items to pseudo-randomly pick from. |
Properties
.value
Item pseudo-randomly picked from the items array.
ItemController<T>.value: T;.items
Array of items to pseudo-randomly pick from.
ItemController<T>.items: T[];ObjectPropertyController
Utility class extending PRNGController for pseudo-randomly picking a property value from an object.
Constructor
| Parameter | Type | Default | Description |
| --------- | ------------------- | ------- | ------------------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| object | Record<string, T> | | Object to pseudo-randomly pick the property value from. |
Properties
.value
Property value pseudo-randomly picked from the object.
ObjectPropertyController<T>.value: T | undefined;.object
Object to pseudo-randomly pick the property value from.
ObjectPropertyController<T>.object: Record<string, T>;WeightsController
Utility class extending PRNGController for pseudo-randomly picking an item from an array of weighted items.
Types
type WeightedItem<T = unknown> = { weight: number; value: T };
type WeightedItems<T = unknown> = Array<WeightedItem<T>>;Constructor
| Parameter | Type | Default | Description |
| --------- | ------------------ | ------- | --------------------------------------------------------- |
| seed | Seed | | Seed string or number used for pseudo-random generations. |
| items | WeightedItems<T> | | Array of weighted items to pseudo-randomly pick from. |
Properties
.value
Item pseudo-randomly picked from the weighted items array.
WeightsController<T>.value: T;.items
Array of weighted items to pseudo-randomly pick from.
WeightsController<T>.items: WeightedItems<T>;.weights
Array of weights.
WeightsController.weights: readonly number[];GaussianController
Utility class extending PRNGController for generating pseudo-random numbers fitting a Gaussian (normal) distribution.
Constructor
| Parameter | Type | Default | Description |
|-----------|-----------|---------|---------------------------------------------------------- |
| seed | Seed | | Seed string or number used for pseudo-random generations. |
| [mean] | number | 0 | Mean (central) value of the distribution. |
| [spread] | number | 1 | Spread (standard deviation) of the distribution. |
Properties
.value
Pseudo-random number value generated by the controller.
GaussianController.value: number;.mean
Mean (central) value for the distribution.
GaussianController.mean: number;.spread
Spread (standard deviation) for the distribution.
GaussianController.spread: number;Group Controllers
PRNGGroupController
Utility abstract class for managing multiple instances of controllers.
- new PRNGGroupController<T, C>(seed)
- .seed:
string - .controllers:
C[] - .createController(index):
C - .getValueAt(index):
T - .dispose():
void
- .seed:
Properties
.seed
Seed string used for pseudo-random generations.
PRNGGroupController.seed: string;.controllers
Controllers managed by the group.
PRNGGroupController<T, C>.controllers: C[];Methods
.createController(index)
Create a new controller.
index: Index of the controller to create.
PRNGGroupController<T, C>.createController(index: number): C;.getValueAt(index)
Generate a pseudo-random value.
index: Index of the controller to use.
PRNGGroupController<T>.getValueAt(index: number): T;.dispose()
Dispose the group controllers.
PRNGGroupController.dispose(): void;BooleanGroupController
Utility class extending PRNGGroupController for managing multiple BooleanController.
Constructor
| Parameter | Type | Default | Description |
| ------------- | ---------| ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| [probability] | number | 0.5 | Probability to get true. |
Properties
.probability
Probability to get true.
BooleanGroupController.probability: number;SignGroupController
Utility class extending PRNGGroupController for managing multiple SignController.
Constructor
| Parameter | Type | Default | Description |
| ------------- | ---------| ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| [probability] | number | 0.5 | Probability to get 1. |
Properties
.probability
Probability to get 1.
SignGroupController.probability: number;FloatGroupController
Utility class extending PRNGGroupController for managing multiple FloatController.
Constructor
| Parameter | Type | Default | Description |
| --------- | ------------------ | ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| [min] | number | 0 | Minimum boundary. |
| [max] | number | 1 | Maximum boundary. |
Properties
.min
Minimum boundary.
FloatGroupController.min: number;.max
Maximum boundary.
FloatGroupController.max: number;IntGroupController
Utility class extending PRNGGroupController for managing multiple IntController.
Constructor
| Parameter | Type | Default | Description |
| --------- | ------------------ | ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| min | number | | Minimum boundary. |
| max | number | | Maximum boundary. |
Properties
.min
Minimum boundary.
IntGroupController.min: number;.max
Maximum boundary.
IntGroupController.max: number;HexColorGroupController
Utility class extending PRNGGroupController for managing multiple HexColorController.
Constructor
| Parameter | Type | Default | Description |
| --------- | -------- | ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
ItemGroupController
Utility class extending PRNGGroupController for managing multiple ItemController.
Constructor
| Parameter | Type | Default | Description |
| --------- | ------ | ------- | ----------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| items | T[] | | Array of items to pseudo-randomly pick from. |
Properties
.items
Array of items to pseudo-randomly pick from.
ItemGroupController<T>.items: T[];ObjectPropertyGroupController
Utility class extending PRNGGroupController for managing multiple ObjectPropertyController.
- new ObjectPropertyGroupController(seed, object)
- .object:
Record<string, T>
- .object:
Constructor
| Parameter | Type | Default | Description |
| --------- | ------------------- | ------- | ------------------------------------------------------- |
| seed | Seed | | Seed string used for pseudo-random generations. |
| object | Record<string, T> | | Object to pseudo-randomly pick the property value from. |
Properties
.object
Object to pseudo-randomly pick the property value from.
ObjectPropertyGroupController<T>.object: Record<string, T>;WeightsGroupController
Utility class extending PRNGGroupController for managing multiple WeightsController.
Constructor
| Parameter | Type | Default | Description |
| --------- | ------------------ | ------- | --------------------------------------------------------- |
| seed | Seed | | Seed string or number used for pseudo-random generations. |
| items | WeightedItems<T> | | Array of weighted items to pseudo-randomly pick from. |
Properties
.items
Array of weighted items to pseudo-randomly pick from.
WeightsGroupController<T>.items: WeightedItems<T>;.weights
Array of weights.
WeightsGroupController.weights: readonly number[];GaussianGroupController
Utility class extending PRNGGroupController for managing multiple GaussianController.
Constructor
| Parameter | Type | Default | Description |
|-----------|-----------|---------|---------------------------------------------------------- |
| seed | Seed | | Seed string or number used for pseudo-random generations. |
| [mean] | number | 0 | Mean (central) value of the distribution. |
| [spread] | number | 1 | Spread (standard deviation) of the distribution. |
Properties
.mean
Mean (central) value for the distribution.
GaussianGroupController.mean: number;.spread
Spread (standard deviation) for the distribution.
GaussianGroupController.spread: number;License
MIT License, see LICENSE for details.

