src/main/java/org/teachingextensions/logo/utils/MazeUtils/StdRandom.java
public static int discrete(double[] a)
  {
    if (a == null)
      throw new NullPointerException("argument array is null");
    double EPSILON = 1E-14;
    double sum = 0.0;
    for (int i = 0; i < a.length; i++)
    {
      if (!(a[i] >= 0.0))
        throw new IllegalArgumentException("array entry " + i + " must be nonnegative: " + a[i]);
      sum = sum + a[i];
    }
    if (sum > 1.0 + EPSILON || sum < 1.0 - EPSILON)
      throw new IllegalArgumentException("sum of array entries does not approximately equal 1.0: " + sum);
    // the for loop may not return a value when both r is (nearly) 1.0 and when the
    // cumulative sum is less than 1.0 (as a result of floating-point roundoff error)
    while (true)
    {
      double r = uniform();
      sum = 0.0;
      for (int i = 0; i < a.length; i++)
      {
        sum = sum + a[i];
        if (sum > r)
          return i;
      }
    }
  }
public static int discrete(double[] a)
  {
    if (a == null)
      throw new NullPointerException("argument array is null");
    double EPSILON = 1E-14;
    double sum = 0.0;
    for (int i = 0; i < a.length; i++)
    {
      if (!(a[i] >= 0.0))
        throw new IllegalArgumentException("array entry " + i + " must be nonnegative: " + a[i]);
      sum = sum + a[i];
    }
    if (sum > 1.0 + EPSILON || sum < 1.0 - EPSILON)
      throw new IllegalArgumentException("sum of array entries does not approximately equal 1.0: " + sum);
    // the for loop may not return a value when both r is (nearly) 1.0 and when the
    // cumulative sum is less than 1.0 (as a result of floating-point roundoff error)
    while (true)
    {
      double r = uniform();
      sum = 0.0;
      for (int i = 0; i < a.length; i++)
      {
        sum = sum + a[i];
        if (sum > r)
          return i;
      }
    }
  }
Returns a random integer from the specified discrete distribution.

@param  a the probability of occurrence of each integer
@return a random integer from a discrete distribution:
<tt>i</tt> with probability <tt>a[i]</tt>
@throws NullPointerException if <tt>a</tt> is <tt>null</tt>
@throws IllegalArgumentException if sum of array entries is not (very nearly) equal to <tt>1.0</tt>
@throws IllegalArgumentException unless <tt>a[i] >= 0.0</tt> for each index <tt>i</tt>