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-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:
TypeError
engine 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
},
});
Engine
| Object
numberGenerator.engine ⇒ 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 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
},
});
number
numberGenerator.integer(min, max) ⇒ 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. |
number
numberGenerator.real(min, max, [inclusive]) ⇒ 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. |
Object
maxEngine : Engine that always returns the maximum value. Used internally for calculating max roll values.
Kind: global constant
Since: 4.2.0
Array.<number>
maxEngine.range : 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];
number
maxEngine.next() ⇒ Returns the maximum number index for the range
Kind: static method of maxEngine
Object
minEngine : Engine that always returns the minimum value. Used internally for calculating min roll values.
Kind: global constant
Since: 4.2.0
number
minEngine.next() ⇒ Returns the minimum number index, 0
Kind: static method of minEngine
Object
engines : 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