vsupalov


Pseudo-random integer number from a range

October 29, 2011 [ javascript | old ]

The following function generates a pseudo-random integer n, with min ≤ n ≤ max. Of course min should be smaller or equal to max. In case of javascript, golang and python the random() function equivalents output values in the range [0, 1).

function randomRange(min, max)  {
  var range = (max-min) + 1;
  var rand = Math.floor( Math.random()*range );
  return min+rand;
}

Want to be notified when I publish new content?

Just enter your name and email below. You will also get content that I share exclusively with the list, and zero spam!