code: protected double rejection(double pmax, double xmin, double xmax) {
          double x;
          double y;
          do {
              x = xmin + Math.random() * (xmax - xmin);
              y = Math.random() * pmax;
          } while (p(x) < y);
  
          return x;
      }
docs: Use the rejection technique to draw a sample from the given distribution.
  WARNING : this simulation technique can take a very long time.
  Rejection sampling is also commonly called the acceptance-rejection
  method or "accept-reject algorithm".
  It generates sampling values from an arbitrary probability distribution
  function f(x) by using an instrumental distribution g(x), under the
  only restriction that f(x) < M g(x) where M > 1 is an appropriate
  bound on f(x) / g(x).
  <p>
  Rejection sampling is usually used in cases where the form of f(x)
  makes sampling difficult. Instead of sampling directly from the
  distribution f(x), we use an envelope distribution M g(x) where
  sampling is easier. These samples from M g(x) are probabilistically
  accepted or rejected.
  <p>
  This method relates to the general field of Monte Carlo techniques,
  including Markov chain Monte Carlo algorithms that also use a proxy
  distribution to achieve simulation from the target distribution f(x).
  It forms the basis for algorithms such as the Metropolis algorithm.