NumberGenerator

Classes

NumberGenerator

The NumberGenerator is capable of generating random numbers.

Constants

maxEngine : Object

Engine that always returns the maximum value. Used internally for calculating max roll values.

minEngine : Object

Engine that always returns the minimum value. Used internally for calculating min roll values.

engines : Object

List of built-in number generator engines.

NumberGenerator

The NumberGenerator is capable of generating random numbers.

Kind: global class
See: This uses random-jsopen in new window. For details of the engines, check the documentationopen in new window.
Since: 4.2.0

new NumberGenerator([engine])

Create a NumberGenerator instance.

The engine can be any object that has a next() method, which returns a number.

Throws:

  • TypeError engine must have function next()
ParamTypeDefaultDescription
[engine]Engine | ObjectnativeMathThe RNG engine to use

Example (Built-in engine)

new NumberGenerator(engines.nodeCrypto);

Example (Custom engine)

new NumberGenerator({
  next() {
    // return a random number
  },
});

numberGenerator.engine ⇒ Engine | Object

The current engine.

Kind: instance property of NumberGenerator

numberGenerator.engine

Set the engine.

The engine can be any object that has a next() method, which returns a number.

Kind: instance property of NumberGenerator
Throws:

  • TypeError engine must have function next()

See: engines

ParamType
engineEngine | Object

Example (Built-in engine)

numberGenerator.engine = engines.nodeCrypto;

Example (Custom engine)

numberGenerator.engine = {
  next() {
    // return a random number
  },
});

numberGenerator.integer(min, max) ⇒ number

Generate a random integer within the inclusive range [min, max].

Kind: instance method of NumberGenerator
Returns: number - The random integer

ParamTypeDescription
minnumberThe minimum integer value, inclusive.
maxnumberThe maximum integer value, inclusive.

numberGenerator.real(min, max, [inclusive]) ⇒ number

Returns a floating-point value within [min, max) or [min, max].

Kind: instance method of NumberGenerator
Returns: number - The random floating-point value

ParamTypeDefaultDescription
minnumberThe minimum floating-point value, inclusive.
maxnumberThe maximum floating-point value.
[inclusive]booleanfalseIf true, max will be inclusive.

maxEngine : Object

Engine that always returns the maximum value. Used internally for calculating max roll values.

Kind: global constant
Since: 4.2.0

maxEngine.range : Array.<number>

The min / max number range (e.g. [1, 10]).

This must be set for the next() method to return the correct last index.

Kind: static property of maxEngine
Example

maxEngine.range = [1, 10];

maxEngine.next() ⇒ number

Returns the maximum number index for the range

Kind: static method of maxEngine

minEngine : Object

Engine that always returns the minimum value. Used internally for calculating min roll values.

Kind: global constant
Since: 4.2.0

minEngine.next() ⇒ number

Returns the minimum number index, 0

Kind: static method of minEngine

engines : Object

List of built-in number generator engines.

Kind: global constant
See: This uses random-jsopen in new window. For details of the engines, check the documentationopen in new window.
Since: 4.2.0