Generate Random Numbers in JavaScript

Posted by yhuang
Public (Editable by Users)
JavaScript
None
Edit
function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

console.log(getRandomInt(3));
// expected output: 0, 1 or 2

console.log(getRandomInt(1));
// expected output: 0

console.log(Math.random());
// expected output: a number between 0 and 1

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