JavaScript Generate Random Integer From Min and Max

April 16, 2020
function getRandomInt(min, max) {
  return Math.floor(Math.random() * ((max+1) - min)) + min
}

Usage

getRandomInt(0,0)   // always 0
getRandomInt(0,1)   // 0-1
getRandomInt(0,10)  // 0-10
getRandomInt(1,10)  // 1-10
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.