// import the moduleimportNumberGeneratorfrom"recoverable-random";// orconstNumberGenerator=require("recoverable-random").default;// create a generator instanceconstgenerator=newNumberGenerator("seed");// generate a random integer between 0 and 9console.log(generator.random(0,10));// >>> 6// generate a random floating-point number between 0 and 9console.log(generator.random(0,10,true));// >>> 1.458055829850764 // get the state code of the generatorconststateCode=generator.getStateCode();console.log(generator.random(0,10));// >>> 6console.log(generator.random(0,10));// >>> 8// recover the state of the generatorgenerator.recoverState(stateCode);console.log(generator.random(0,10));// >>> 6console.log(generator.random(0,10));// >>> 8
📖 API Reference
Constructor
Creates a new instance of the NumberGenerator class.
newNumberGenerator(seed?: number|string)
Parameter
Optional
Type
Default
Description
seed
Yes
number | string
undefined
The seed value of the generator.
Static Methods
Converts a string to seed.
NumberGenerator.stringToSeed(str: string)
Parameter
Optional
Type
Default
Description
str
No
string
-
The string to be converted.
Return Type
Description
number
The seed value.
Methods
Generates a pseudorandom number within the specified range.
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.
Gets the state code of the generator.
getStateCode()
Return Type
Description
string
The state code of the generator.
Recovers the state of the generator.
recoverState(stateCode: string)
Parameter
Optional
Type
Default
Description
stateCode
No
string
-
The state code of the generator.
Sets the seed of the generator.
setSeed(seed: number|string)
Parameter
Optional
Type
Default
Description
seed
No
number | string
-
The seed value of the generator.
🤝 Contributing
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.
📝 License
This project is licensed under the MIT License - see the LICENSE file for details
☕ Donation
Love the program? Consider a donation to support my work.