Pseudo-random integer number from a range

October 29, 2011

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?

Enter your email below to join my mailing list!