XORShift32

XORShift32 Pseudorandom Number Generator Class

Constructor

new XORShift32()

Implements the XORShift32 algorithm to generate pseudorandom numbers.

This class provides the XORShift32 method of generating psuedorandom numbers to the PRNG class. It uses bitwise operations to generate a sequence of pseudorandom 32-bit unsigned integers.

The XORShift algorithm was written by Geogrge Marsaglia in 2003 and is released to the public domain, meaning it can be freely used, modified, and distributed without restrictions.

See
  • Marsaglia, G. (2003) "XORShift RNGs", Journal of Statistical Software https://www.jstatsoft.org/article/view/v008i14
Example
const rng = new XORShift32(123456789);
console.log(rng.next()); // Generates a pseudorandom number

Extends

Classes

XORShift32

Implements the XORShift32 algorithm to generate pseudorandom numbers.

Methods

maxValue() → {number}

Returns the maximum possible value of XORShift32.

Overrides
Returns:

The maximum value (2^32).

Type: 
number

next() → {number}

Generates the next psuedo-random number.

Overrides
Returns:

A number in the range [0-1).

Type: 
number
Example
// Returns a random number in the range [0-1)
const rng = new XORShift32();
console.log(rng.next());

randomBipolarFloat() → {number}

Generate a random float in the range [-1, 1).

Returns:
  • A number between -1 (inclusive) and 1 (exclusive).
Type: 
number

randomChance(chanceopt) → {boolean}

Returns a boolean based on a specified probability.

Parameters:
NameTypeAttributesDefaultDescription
chancenumber<optional>
0.5

The probability of returning true (between 0 and 1).

Returns:
  • True if the random float is less than the chance, otherwise false.
Type: 
boolean

randomElement(array) → {*}

Select a random element from an array.

Parameters:
NameTypeDescription
arrayarray

The array from which to select an element.

Returns:
  • A randomly selected element from the array.
Type: 
*

randomFloat() → {number}

Generate a random float in the range [0, 1).

Returns:
  • A number between 0 (inclusive) and 1 (exclusive).
Type: 
number

randomInteger(min, max) → {number}

Generate a random integer from a specified range of values.

Parameters:
NameTypeDescription
minnumber

The minimum integer value (inclusive).

maxnumber

The maximum integer value (exclusive).

Returns:
  • A random integer between min (inclusive) and max (exclusive).
Type: 
number

randomWeighted(choices) → {function|number|string}

Selects an option probabalistically from a set of weighted choices.

Parameters:
NameTypeDescription
choicesArray.<Object>

An array of objects with option and weight properties.

Properties
NameTypeDescription
optionfunction | number | string

The outcome of the choice.

weightnumber

The weight of the choice. Higher weights (relative to the other choices) increase the likelihood of selection.

Returns:
  • The selected option.
Type: 
function | number | string