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

        if (p < Math.exp(-lambda)) {
            return 0;
        }

        int n = (int) Math.max(Math.sqrt(lambda), 5.0);
        int nl, nu, inc = 1;

        if (p < cdf(n)) {
            do {
                n = Math.max(n - inc, 0);
                inc *= 2;
            } while (p < cdf(n) && n > 0);
            nl = n;
            nu = n + inc / 2;
        } else {
            do {
                n += inc;
                inc *= 2;
            } while (p > cdf(n));
            nu = n;
            nl = n - inc / 2;
        }

        return quantile(p, nl, nu);
    }
@Test
    public void testQuantile() {
        System.out.println("quantile");
        PoissonDistribution instance = new PoissonDistribution(3.5);
        instance.rand();
        assertEquals(0, instance.quantile(0.01), 1E-7);
        assertEquals(1, instance.quantile(0.1), 1E-7);
        assertEquals(2, instance.quantile(0.2), 1E-7);
        assertEquals(2, instance.quantile(0.3), 1E-7);
        assertEquals(3, instance.quantile(0.4), 1E-6);
        assertEquals(3, instance.quantile(0.5), 1E-6);
        assertEquals(6, instance.quantile(0.9), 1E-6);
        assertEquals(8, instance.quantile(0.99), 1E-6);
    }