1.0.5 • Published 3 months ago

recoverable-random v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

recoverable-random

Tests MIT License   Donation

npm version npm downloads

A recoverable number generator that generates pseudorandom integer or floating-point numbers within a given range.

🗝️ Features

  • Recoverable
  • Supports float and integer
  • Supports seed
  • Supports TypeScript
  • Zero dependencies

🚀 Getting Started

Installation

npm install recoverable-random

Visit the npm page.

Import

// ES6
import NumberGenerator from "recoverable-random";

// CommonJS
const NumberGenerator = require("recoverable-random").default;

Usage Example~ 🍀

// 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

📖 API Reference

Constructor

Creates a new instance of the NumberGenerator class.

new NumberGenerator(seed?: number | string)
ParameterOptionalTypeDefaultDescription
seedYesnumber | stringundefinedThe seed value of the generator.

Static Methods

Converts a string to seed.

NumberGenerator.stringToSeed(str: string)
ParameterOptionalTypeDefaultDescription
strNostring-The string to be converted.
Return TypeDescription
numberThe seed value.

Methods

Generates a pseudorandom number within the specified range.

random(min: number, max: number, isFloat?: boolean)
ParameterOptionalTypeDefaultDescription
minNonumber-The minimum value of the range.
maxNonumber-The maximum value of the range.
isFloatYesbooleanfalseWhether the generated number is a floating-point number.
Return TypeDescription
numberThe generated number.

Gets the state code of the generator.

getStateCode()
Return TypeDescription
stringThe state code of the generator.

Recovers the state of the generator.

recoverState(stateCode: string)
ParameterOptionalTypeDefaultDescription
stateCodeNostring-The state code of the generator.

Sets the seed of the generator.

setSeed(seed: number | string)
ParameterOptionalTypeDefaultDescription
seedNonumber | 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.

"Donation" <- click me~

1.0.5

3 months ago

1.0.4

3 months ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago