code: protected double quantile(double p, int xmin, int xmax) {
          while (xmax - xmin > 1) {
              int xmed = (xmax + xmin) / 2;
              if (cdf(xmed) > p) {
                  xmax = xmed;
              } else {
                  xmin = xmed;
              }
          }
  
          if (cdf(xmin) >= p)
              return xmin;
          else
              return xmax;
      }
docs: Invertion of cdf by bisection numeric root finding of "cdf(x) = p"
  for discrete distribution.* Returns integer n such that
  P(<n) &le; p &le; P(<n+1).