Sampler

The Sampler class takes a RangeGroup and does some pre-processing to allow fast ordered or random sampling. You provide a number between [0,1), which is mapped to the RangeGroup#size elements of the group to return a sample. E.g. 0.5 gives you the median element, while 1.0 gives the max. Specifying a random number gives you a random element inside the group. This assumes the RangeGroup will not be modified, as its internal cache is not updated when the group updates.

Usage:

const s = new Sampler(my_group);
const a = s.sample();
// or using a custom random generator
const b = s.sample(pareto_distribution());

Constructor

new Sampler(group)

Create a new Sampler

Parameters:
NameTypeDescription
groupRangeGroup

the range group whose ranges we should sample from; it should be normalized and non-empty

Members

type :RangeType

Range type to use for sampling, taken from the originating RangeGroup

Methods

sample(iopt, nullable) → {any}

Draw a sample. This uses binary search (O(log(N))) to find an appropriate range, before calling RangeType.sample to fetch the actual sample

Parameters:
NameTypeAttributesDefaultDescription
inumber<optional>
<nullable>
null

Number between [0,1), representing percentile into the group. If null, a uniform random number is generated via Math.random. This determines which sample should be returned. If not in [0,1), it will be clamped to be so.

Returns:

randomly drawn sample

Type: 
any