random-integers
v1.0.1
Published
A **high-performance, robust, and versatile** random integer generator for JavaScript and TypeScript.
Maintainers
Readme
random-integer
A high-performance, robust, and versatile random integer generator for JavaScript and TypeScript.
Features & Improvements over basic implementations
Single-argument support
randomInteger(5)generates numbers from0to5.
Automatic min/max swap
- Handles reversed arguments:
randomInteger(10, 2)→2–10.
- Handles reversed arguments:
Robust numeric validation
- Throws a
TypeErrorif arguments are not finite numbers. - Prevents
NaNandInfinityfrom producing invalid results.
- Throws a
High-performance integer generation
- Uses bitwise truncation
(x | 0)for faster integer conversion in tight loops. - Ideal for gaming, simulations, or high-stakes computational scenarios.
- Uses bitwise truncation
Clean, modern API
- Minimal, readable, and maintainable code.
- Works in both Node.js and browser environments.
TypeScript ready
- Fully typed function for safer development and autocompletion.
Installation
npm install random-integer
# or
yarn add random-integer
import randomInteger from 'random-integer';
// Single argument
console.log(randomInteger(5)); // 0–5
// Two arguments
console.log(randomInteger(2, 10)); // 2–10
// Arguments can be reversed
console.log(randomInteger(10, 2)); // 2–10
Why Choose This Package?
• ✅ Fast: Optimized for millions of calls per second.
• ✅ Safe: Checks for invalid numbers.
• ✅ Flexible: Handles single/multiple arguments, inclusive ranges.
• ✅ Ready for production: Minimal dependencies, lightweight, reliable.
⸻
Funding
If you find this library useful and want to support its maintenance and future improvements, please consider contributing via GitHub Sponsors or Buy Me a Coffee. Every contribution helps make the library faster, safer, and better maintained for high-stakes scenarios.
⸻