Function cmb_random_discrete_nonuniform

Function Documentation

uint64_t cmb_random_discrete_nonuniform(uint64_t n, const double *pa)

A non-uniform discrete distribution among n alternatives. It returns the selected array index i on [0, n-1] with a probability pa[i].

This function uses a very simple O(n) implementation. For anything larger than ~15 values, use the alias sampling method below instead.

Both can easily be extended to arbitrary discrete values by letting the result be an index into an array of whatever values need to be selected.

See the implementations of the normal and exponential distributions in cmb_random.c for an example of alias sampling from a table of values.

Parameters:
  • n – Number of entries

  • pa – The probabilities for each of the entries, array size n. The probabilities in pa[] should sum to 1.0, i.e., mutually exclusive, collectively exhaustive.