How to get a random number between two numbers

Learn how to get a random number between two numbers in JavaScript.

Random number

The following JavaScript function always returns a random number between the minimum (inclusive) and maximum (exclusive):

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min) ) + min;
}

Try It Yourself

The following JavaScript function always returns a random number between the minimum and maximum values (both inclusive):

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

Try It Yourself

Related Pages

Tutorial:JavaScript Random