Javascript Math random() get random integer between two values, inclusive

Introduction

The maximum is inclusive and the minimum is inclusive

function getRandomIntInclusive(min, max) {
   min = Math.ceil(min);/*w w  w . j av  a  2 s . c  o m*/
   max = Math.floor(max);
   return Math.floor(Math.random() * (max - min + 1)) + min; 
   
}
console.log(getRandomIntInclusive(1, 3));
console.log(getRandomIntInclusive(1, 3));
console.log(getRandomIntInclusive(1, 3));
console.log(getRandomIntInclusive(1, 3));
console.log(getRandomIntInclusive(1, 3));



PreviousNext

Related