@Override
    public double quantile(double p) {
        if (p < 0.0 || p > 1.0) {
            throw new IllegalArgumentException("Invalid p: " + p);
        }

        if (p == 0.0) {
            return 0;
        }

        if (p == 1.0) {
            return n;
        }

        // Starting guess near peak of density.
        // Expand interval until we bracket.
        int kl, ku, inc = 1;
        int k = Math.max(0, Math.min(n, (int) (n * p)));
        if (p < cdf(k)) {
            do {
                k = Math.max(k - inc, 0);
                inc *= 2;
            } while (p < cdf(k) && k > 0);
            kl = k;
            ku = k + inc / 2;
        } else {
            do {
                k = Math.min(k + inc, n + 1);
                inc *= 2;
            } while (p > cdf(k));
            ku = k;
            kl = k - inc / 2;
        }

        return quantile(p, kl, ku);
    }
@Test
    public void testQuantile() {
        System.out.println("quantile");
        BinomialDistribution instance = new BinomialDistribution(100, 0.3);
        instance.rand();
        assertEquals(0, instance.quantile(0), 1E-7);
        assertEquals(0, instance.quantile(0.00000000000000001), 1E-7);
        assertEquals(17, instance.quantile(0.001), 1E-7);
        assertEquals(20, instance.quantile(0.01), 1E-7);
        assertEquals(24, instance.quantile(0.1), 1E-7);
        assertEquals(26, instance.quantile(0.2), 1E-7);
        assertEquals(30, instance.quantile(0.5), 1E-7);
        assertEquals(36, instance.quantile(0.9), 1E-7);
        assertEquals(100, instance.quantile(1.0), 1E-7);
    }