xorshift.js

v0.1.1
xorshift* / xorshift+ generators
random prng xorshift xorshift128+ xorshift128plus xorshift1024* xorshift1024star

#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

var xorshift = require('xorshift.js')

for (var 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.

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

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

XorShift1024Star(seed, p)

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

var XorShift128Plus = require('xorshift.js').XorShift128Plus
var 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]
var prng1 = new XorShift1024Star(seed, 1)
var 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.

var XorShift128Plus = require('xorshift.js').XorShift128Plus
var 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.

var XorShift128Plus = require('xorshift.js').XorShift128Plus
var 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.

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

License

MIT

Metadata

  • MIT
  • Whatever
  • Andreas Madsen
  • released 2/12/2016

Downloads

Maintainers