Function cmb_random_discrete_nonuniform
Defined in File cmb_random.h
Function Documentation
-
uint64_t cmb_random_discrete_nonuniform(uint64_t n, const double *pa)
A non-uniform discrete distribution among
nalternatives. It returns the selected array indexion[0, n-1]with a probabilitypa[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.cfor 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 inpa[]should sum to 1.0, i.e., mutually exclusive, collectively exhaustive.