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