Derrick Jones
Derrick Jones

Reputation: 11

Generating Samples from Distribution

I am in the process of learning about statistics, and let's say I have an outcome from some experiment:

1 | 0.34
2 | 0.10
3 | 0.05
4 | 0.13
5 | 0.13
6 | 0.25

I am interested generating samples using a uniform random number generator from this distribution. Any suggestions?

Upvotes: 0

Views: 43

Answers (1)

David Wright
David Wright

Reputation: 805

This is a very standard problem with a very standard solution. Form an array where each entry contains not the probability of that index, but the sum of all probabilities up to that index. For your example problem, the array is p[1] = 0.34, p[2] = 0.44, p[3] = 0.49, etc. Use your uniform RNG to generate u between 0 and 1. Then find the index i such that p[i-1] < u < p[i]. For a very small array like this, you can use linear search, but for a large array you will want to use binary search. Notice that you can re-use the array to generate multiple deviates, so don't re-form it every time.

Upvotes: 1

Related Questions