public double frequencyBasedSample(SplittableRandom rng, long totalObservations) {
        if ((totalObservations != this.totalObservations) || (cdf == null)) {
            regenerateCDF(totalObservations);
        }
        int lookup = Util.sampleFromCDF(cdf,rng);
        return values[lookup];
    }
@Test
    public void samplingTest() {
        SplittableRandom rng = new SplittableRandom(1);

        CategoricalInfo c;

        c = generateEmptyInfo();

        c.frequencyBasedSample(rng, 50);

        assertEquals(1,c.values.length);
        assertEquals(0,c.values[0],DELTA);
        assertEquals(1,c.cdf.length);
        assertEquals(1.0,c.cdf[0],DELTA);

        for (int i = 0; i < 50; i++) {
            assertEquals(0.0,c.frequencyBasedSample(rng, 50),DELTA);
        }

        c = generateOneValueInfo();

        c.frequencyBasedSample(rng, 50);

        assertEquals(2,c.values.length);
        assertEquals(0,c.values[0],DELTA);
        assertEquals(5,c.values[1],DELTA);
        assertEquals(2,c.values.length);
        assertEquals(0.5,c.cdf[0],DELTA);
        assertEquals(1.0,c.cdf[1],DELTA);

        double sum = 0;
        double posCount = 0.0;
        for (int i = 0; i < NUM_SAMPLES; i++) {
            double sample = c.frequencyBasedSample(rng, 50);
            if (sample > DELTA) {
                posCount++;
            }
            sum += sample;
        }
        assertEquals(0.5,posCount/NUM_SAMPLES,1e-1);
        assertEquals(2.5,sum/NUM_SAMPLES,1e-1);

        c = generateOneValueInfo();

        c.frequencyBasedSample(rng,1000);

        assertEquals(2,c.values.length);
        assertEquals(0,c.values[0],DELTA);
        assertEquals(5,c.values[1],DELTA);
        assertEquals(2,c.values.length);
        assertEquals(0.975,c.cdf[0],DELTA);
        assertEquals(1.0,c.cdf[1],DELTA);

        c = generateFullInfo();

        c.frequencyBasedSample(rng,50);

        assertEquals(5,c.values.length);
        assertEquals(5,c.cdf.length);
        checkValueAndProb(c,0.0,0.6);
        checkValueAndProb(c,-1.0,0.1);
        checkValueAndProb(c,2.0,0.1);
        checkValueAndProb(c,3.0,0.1);
        checkValueAndProb(c,4.0,0.1);

        c = generateFullInfo();

        c.frequencyBasedSample(rng, 100);

        assertEquals(5,c.values.length);
        assertEquals(5,c.cdf.length);
        checkValueAndProb(c,0.0,0.8);
        checkValueAndProb(c,-1.0,0.05);
        checkValueAndProb(c,2.0,0.05);
        checkValueAndProb(c,3.0,0.05);
        checkValueAndProb(c,4.0,0.05);
    }