NumberGenerator
Classes
- NumberGenerator
The
NumberGeneratoris 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-js. For details of the engines, check the documentation.
Since: 4.2.0
- NumberGenerator
- new NumberGenerator([engine])
- .engine ⇒
Engine|Object - .engine
- .integer(min, max) ⇒
number - .real(min, max, [inclusive]) ⇒
number
new NumberGenerator([engine])
Create a NumberGenerator instance.
The engine can be any object that has a next() method, which returns a number.
Throws:
TypeErrorengine must have functionnext()
| Param | Type | Default | Description |
|---|---|---|---|
| [engine] | Engine | Object | nativeMath | The 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:
TypeErrorengine must have functionnext()
See: engines
| Param | Type |
|---|---|
| engine | Engine | 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
| Param | Type | Description |
|---|---|---|
| min | number | The minimum integer value, inclusive. |
| max | number | The 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
| Param | Type | Default | Description |
|---|---|---|---|
| min | number | The minimum floating-point value, inclusive. | |
| max | number | The maximum floating-point value. | |
| [inclusive] | boolean | false | If 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-js. For details of the engines, check the documentation.
Since: 4.2.0