Just wondering what type of algorithm this is,
or if there is an easier/more efficient way to go about this:
Say we are given a certain probability density, say
prob[] = {.1, .15, .25, .05, .45}
Group 1 - 10%
Group 2 - 15%
Group 3 - 25%
Group 4 - 5%
Group 5 - 45%
and a random number, (0,1),
ran = .853234
Insert into one of the 5 groups
if (ran <=prob[0]) selection = 1;
else if (ran <= prob[0]+prob[1]) selection = 2;
...
else if (ran <= prob[0]+prob[1]+...+prob[4]) selection = 5;
I am not very well-versed on random number generation
What you are essentially doing here is inverting the cumulative distribution function. Let F
be the CDF of a random variable X
with a given distribution, then it is defined as F(x) == P[X <= x]
.
The very useful thing here, is that if you generate a uniform random variable U
between 0 and 1, then
P[F^-1(U) <= x] == P[U <= F(x)] == F(x) == P[X <= x]
which means that F^-1(U)
will have the same distribution as X
!
Of course this is only possible if you can invert the CDF, but in your case F
is a piecewise function (like a staircase), and your algorithm determines, for a given uniform value, at which step this value is met. Your algorithm is therefore perfectly correct.
However, you could improve it if you have a lot of random numbers to generate: first generate the CDF table, which in your case would be
CDF[] = {.1, .25, .5, .55, 1.}
then for each generated uniform number between 0 and 1, simply perform a dichotomy on the CDF table to retriver the corresponding index.