Javascript Math random() get random integer between two values

Introduction

The value is no lower than min, and is less than (but not equal to) max.

The maximum is exclusive and the minimum is inclusive

function getRandomInt(min, max) {
   min = Math.ceil(min);//from w  ww.  ja  va 2s.c  o m
   max = Math.floor(max);
   return Math.floor(Math.random() * (max - min)) + min; 
}
console.log(getRandomInt(1, 100) );
console.log(getRandomInt(1, 100) );
console.log(getRandomInt(1, 100) );
console.log(getRandomInt(1, 100) );
console.log(getRandomInt(1, 100) );



PreviousNext

Related