Class CompositeSamplers


  • public final class CompositeSamplers
    extends Object
    Factory class to create a sampler that combines sampling from multiple samplers.

    The composite sampler is constructed using a builder for the type of samplers that will form the composite. Each sampler has a weight in the composition. Samples are returned using a 2 step algorithm:

    1. Select a sampler based on its weighting
    2. Return a sample from the selected sampler

    The weights used for each sampler create a discrete probability distribution. This is sampled using a discrete probability distribution sampler. The builder provides methods to change the default implementation.

    The following example will create a sampler to uniformly sample the border of a triangle using the line segment lengths as weights:

     UniformRandomProvider rng = RandomSource.KISS.create();
     double[] a = {1.23, 4.56};
     double[] b = {6.78, 9.01};
     double[] c = {3.45, 2.34};
     ObjectSampler<double[]> sampler =
         CompositeSamplers.<double[]>newObjectSamplerBuilder()
             .add(LineSampler.of(rng, a, b), Math.hypot(a[0] - b[0], a[1] - b[1]))
             .add(LineSampler.of(rng, b, c), Math.hypot(b[0] - c[0], b[1] - c[1]))
             .add(LineSampler.of(rng, c, a), Math.hypot(c[0] - a[0], c[1] - a[1]))
             .build(rng);
     
    Since:
    1.4