vsupalov

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;
}

Join the Mailing List

Subscribe to get weekly updates and my latest articles by email.

    (About the content, privacy, analytics and revocation).

    We won't send you spam. Unsubscribe at any time.

    Powered By ConvertKit