toosoon-prng-controllers
Version:
This project provides PRNG functions with a set of Controllers for generating pseudo-random values using a seed-based approach and various algorithms
65 lines (64 loc) • 1.76 kB
JavaScript
import { PRNG as BasePRNG } from 'toosoon-prng';
/**
* Utility class for generating pseudo-random values and managing PRNG Controllers
*
* @exports
* @class PRNG
* @extends BasePRNG
*/
export class PRNG extends BasePRNG {
controllers = [];
set seed(seed) {
if (this._seed === `${seed}`)
return;
this._seed = `${seed}`;
this.controllers.forEach((controller) => controller.getValue());
}
set algorithm(algorithm) {
if (this._algorithm === algorithm)
return;
this._algorithm = algorithm;
this.controllers.forEach((controller) => controller.getValue());
}
/**
* Add a controller to this PRNG
*
* @param {PRNGController} controller
*/
addController(controller) {
this.controllers.push(controller);
}
/**
* Remove a controller from this PRNG
*
* @param {PRNGController} controller
*/
removeController(controller) {
const index = this.controllers.indexOf(controller);
this.controllers.splice(index, 1);
}
/**
* Set this PRNG seed
*
* @param {string|number} seed
*/
setSeed(seed) {
if (this.seed === `${seed}`)
return;
super.setSeed(seed);
this.controllers.forEach((controller) => controller.getValue());
}
/**
* Set this PRNG algorithm
*
* @param {AlgorithmName} algorithmName Algorithm name
*/
setAlgorithm(algorithmName) {
if (this.algorithm === this.getAlgorithmByName(algorithmName))
return;
super.setAlgorithm(algorithmName);
this.controllers.forEach((controller) => controller.getValue());
}
}
const prng = new PRNG();
export default prng;