JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 81
  • Score
    100M100P100Q67445F
  • License MIT

xorshift* / xorshift+ generators

Package Exports

  • xorshift.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (xorshift.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

#xorshift.js

NPM Package Build Status

Pseudorandom number generator using xorshift (xorshift128+ and xorshift1024*)

This package is based on xorshift [github] with fixed random and added randomBytes & XorShift1024Star.

Installation

npm install xorshift.js

Example

const xorshift = require('xorshift.js')

for (let i = 0; i < 10; i++) {
  console.log(xorshift.random()) // number in range [0, 1)
}

API

This module exports a default PRNG created from XorShift128Plus with seed that have been set from Date.now and Math.random.

XorShift128Plus(seed)

Construct xorshift128+ with specific seed.

const { XorShift128Plus } = require('xorshift.js')
const prng1 = new XorShift128Plus([1, 0, 2, 0])
const prng2 = new XorShift128Plus('000000010000000000000002')

assert(prng1.random() === prng2.random())

XorShift1024Star(seed, p)

Construct xorshift1024* with specific seed (longer period, but slower).

const { XorShift128Plus } = require('xorshift.js')
const seed = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
const prng1 = new XorShift1024Star(seed, 1)
const prng2 = new XorShift1024Star(seed, 1)

assert(prng1.random() === prng2.random())

randomInt64()

This method returns a random 64-bit integer represented as array with two 32-bit numbers.

const { XorShift128Plus } = require('xorshift.js')
const prng = new XorShift128Plus([1, 0, 2, 0])
console.log(prng.randomInt64()) // [8388677, 32896]

random()

This method returns a floating-point, pseudo-random number in the range [0, 1). Like Math.ramdom.

const { XorShift128Plus } = require('xorshift.js')
const prng = new XorShift128Plus([1, 0, 2, 0])
console.log(prng.random()) // 0.0019531410653161885

randomBytes(size)

Generates pseudo-random data. The size argument is a number indicating the number of bytes to generate.

const { XorShift128Plus } = require('xorshift.js')
const prng = new XorShift128Plus([1, 0, 2, 0])
console.log(prng.randomBytes(10).toString('hex')) // 00800045000080800200

License

MIT