Oliver Jumpertz

Random Integer

Category: JavaScript

Share this snippet to:

function randomInt(min = 0, max = 1) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

Usage

You can just call the function any time. Its parameters are optional, so you can decide where to place your lower and upper bounds. As it is an integer, you also only get integers, so no decimal places.

If you’re looking for a random number instead, take a look here

// Generates a random integer between 0 and 1
const rndOne = randomInt();
// Generates a random integer between 0 and 10
const rndTwo = randomInt(0, 10);

Share this snippet to: