Oliver Jumpertz

Random Number

Category: JavaScript

Share this snippet to:

function randomNumber(min = 0, max = 1) {
return Math.random() * (max - min) + 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.

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

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

Share this snippet to: