Tuesday, April 2, 2013

Javascript: random number in a given range

// Returns a random integer between min and max
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

Source:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/random

No comments: