pseudo-random.js

1.3.4 • Public • Published

pseudo-random.js

Library for generating pseudo-random numbers.

INDEX

  1. Installation
  2. Usage
  3. API
    1. random.seed
    2. random.digits
    3. constructor()
    4. next()
    5. shuffleArray()
    6. seedSortArray()
    7. seedUnSortArray()
    8. resetSeed()
  4. License

Installation

$ npm install pseudo-random.js

Usage

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]

API

random.seed

change the seed value. If referenced, you can get the current value.

const seed = random.seed;  // current value
random.seed = 128;

random.digits

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;

constructor()

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)

next()

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

shuffleArray()

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

seedSortArray()

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

seedUnSortArray()

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

resetSeed()

seed is reset to the value passed to the constructor

random.resetSeed();

License

The MIT License.

Package Sidebar

Install

npm i pseudo-random.js

Weekly Downloads

29

Version

1.3.4

License

MIT

Unpacked Size

15.3 kB

Total Files

10

Last publish

Collaborators

  • katsube