@js-util/random-int

1.0.0 • Public • Published

Random Int

Returns a random int between the max (exclusive) and min (inclusive) bounds

npm install

npm install --save @js-util/random-int

Example usage

// Importing the module
const randomInt = require("@js-util/random-int");

// Get some random value
let value = randomInt(0, 100);

Its code

/**
 * Returns a random int between the min, and max bound
 * 
 * @param {int} min value to generate, inclusively (meaning its possible to return min)
 * @param {int} max value to generate, exclusively (meaning it never returns max, unless max == min)
 * 
 * @return {int} random int between max (exclusive) and min (inclusive)
 */
function randomInt(min, max) {
	min = Math.ceil(min);
	max = Math.floor(max);
	return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}

// Export the function
module.exports = randomInt;

Readme

Keywords

Package Sidebar

Install

npm i @js-util/random-int

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

3.07 kB

Total Files

4

Last publish

Collaborators

  • picocreator