Library for generating pseudo-random numbers.
$ npm install pseudo-random.js
const pseudoRandom = require('pseudo-random.js');
const seed = 123;
const random = new pseudoRandom(seed);
//-------------------------------
// Generate
//-------------------------------
random.next(); // 0.05236359
random.next(1, 10); // 2 (1 <= x <= 10)
//-------------------------------
// Shuffle array
//-------------------------------
const array1 = [1, 2, 3, 4, 5];
const r1 = random.shuffleArray(array1); // [3, 5, 1, 4, 2]
//-------------------------------
// Reversible shuffling of array
// (v1.3.0 later)
//-------------------------------
const array2 = [1, 2, 3, 4, 5];
const result1 = random.seedSortArray(array2); // [2, 3, 4, 5, 1]
const result2 = random.seedUnSortArray(result1); // [1, 2, 3, 4, 5]
change the seed value. If referenced, you can get the current value.
const seed = random.seed; // current value
random.seed = 128;
a random number of digits, an integer from 1 to 8. default is 8. If referenced, you can get the current value.
const digits = random.digits; // current value
random.digits = 6;
seed value can be specified at instance creation.
const random = new pseudoRandom(123);
If no seed value is specified, the current time is used.
const random = new pseudoRandom();
console.log(random.seed);
// UNIX TIME(1692199462509)
// ↓
// Last 9 digits (199462509)
// ↓
// Reverse (905264991)
Generates a pseudo-random number from the current seed value. Returns a value with 8 decimal places. seed changes when executed.
const r = random.next();
console.log(r); // 0.12345678
Max and Min values can be specified. The generated values are integers. raodom.digits
are ignored.
const r = random.next(3, 8);
console.log(r); // 5
Shuffle the array using the seed value. Cannot be undone.
const r = random.shuffleArray([1,2,3,4,5]);
console.log(r); // [3, 5, 1, 4, 2]
- Before execution, seed is reset to the value passed to the constructor
Shuffle the array using the seed value. You can use seedUnSortArray()
to undo this.
const r = random.seedSortArray([1,2,3,4,5]);
console.log(r); // [3, 5, 1, 4, 2]
- Before execution, seed is reset to the value passed to the constructor
Restores an array shuffled by seedSortArray()
const r1 = random.seedSortArray([1,2,3,4,5]);
const r2 = random.seedUnSortArray(r1);
console.log(r2); // [1,2,3,4,5]
- Before execution, seed is reset to the value passed to the constructor
seed is reset to the value passed to the constructor
random.resetSeed();
The MIT License.