random2 method

double random2(
  1. double low,
  2. double high
)

( begin auto-generated from random.xml )

Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. If one parameter is passed to the function it will return a float between zero and the value of the high parameter. The function call random(5) returns values between 0 and 5 (starting at zero, up to but not including 5). If two parameters are passed, it will return a float with a value between the the parameters. The function call random(-5, 10.2) returns values starting at -5 up to (but not including) 10.2. To convert a floating-point random number to an integer, use the int() function.

( end auto-generated ) @webref math:random @param low lower limit @param high upper limit @see PApplet#randomSeed(long) @see PApplet#noise(float, float, float)

Implementation

double random2(double low, double high) {
  if (low >= high) return low;
  double diff = high - low;
  double value = 0;
  // because of rounding error, can't just add low, otherwise it may hit high
  // https://github.com/processing/processing/issues/4551
  do {
    value = random(diff) + low;
  } while (value == high);
  return value;
}