A recoverable number generator that generates pseudorandom integer or floating-point numbers within a given range.
- Recoverable
- Supports float and integer
- Supports seed
- Supports TypeScript
- Zero dependencies
npm install recoverable-random
Visit the npm page.
// ES6
import NumberGenerator from "recoverable-random";
// CommonJS
const NumberGenerator = require("recoverable-random").default;
// import the module
import NumberGenerator from "recoverable-random";
// or
const NumberGenerator = require("recoverable-random").default;
// create a generator instance
const generator = new NumberGenerator("seed");
// generate a random integer between 0 and 9
console.log(generator.random(0, 10));
// >>> 6
// generate a random floating-point number between 0 and 9
console.log(generator.random(0, 10, true));
// >>> 1.458055829850764
// get the state code of the generator
const stateCode = generator.getStateCode();
console.log(generator.random(0, 10));
// >>> 6
console.log(generator.random(0, 10));
// >>> 8
// recover the state of the generator
generator.recoverState(stateCode);
console.log(generator.random(0, 10));
// >>> 6
console.log(generator.random(0, 10));
// >>> 8
new NumberGenerator(seed?: number | string)
Parameter | Optional | Type | Default | Description |
---|---|---|---|---|
seed | Yes | number | string | undefined | The seed value of the generator. |
NumberGenerator.stringToSeed(str: string)
Parameter | Optional | Type | Default | Description |
---|---|---|---|---|
str | No | string | - | The string to be converted. |
Return Type | Description |
---|---|
number | The seed value. |
random(min: number, max: number, isFloat?: boolean)
Parameter | Optional | Type | Default | Description |
---|---|---|---|---|
min | No | number | - | The minimum value of the range. |
max | No | number | - | The maximum value of the range. |
isFloat | Yes | boolean | false | Whether the generated number is a floating-point number. |
Return Type | Description |
---|---|
number | The generated number. |
getStateCode()
Return Type | Description |
---|---|
string | The state code of the generator. |
recoverState(stateCode: string)
Parameter | Optional | Type | Default | Description |
---|---|---|---|---|
stateCode | No | string | - | The state code of the generator. |
setSeed(seed: number | string)
Parameter | Optional | Type | Default | Description |
---|---|---|---|---|
seed | No | number | string | - | The seed value of the generator. |
Contributions are welcome! If you find a bug or have a feature request, please open an issue. If you want to contribute code, please fork the repository and submit a pull request.
Before you open a pull request, please make sure that you run npm run dev:test
to make sure the code run as expected.
This project is licensed under the MIT License - see the LICENSE file for details
Love the program? Consider a donation to support my work.