@graphty/layout
Version:
graph layout algorithms based on networkx
42 lines • 1.22 kB
JavaScript
/**
* Random number generator for seed-based randomization
* Maintains exact same functionality as original implementation
*/
export class RandomNumberGenerator {
constructor(seed) {
this.seed = seed || Math.floor(Math.random() * 1000000);
this.m = 2 ** 35 - 31;
this.a = 185852;
this.c = 1;
this._state = this.seed % this.m;
}
_next() {
this._state = (this.a * this._state + this.c) % this.m;
return this._state / this.m;
}
rand(shape = null) {
if (shape === null) {
return this._next();
}
if (typeof shape === 'number') {
const result = [];
for (let i = 0; i < shape; i++) {
result.push(this._next());
}
return result;
}
if (shape.length === 1) {
const result = [];
for (let i = 0; i < shape[0]; i++) {
result.push(this._next());
}
return result;
}
const result = [];
for (let i = 0; i < shape[0]; i++) {
result.push(this.rand(shape.slice(1)));
}
return result;
}
}
//# sourceMappingURL=random.js.map