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
Fbe the CDF of a random variableXwith a given distribution, then it is defined asF(x) == P[X <= x].The very useful thing here, is that if you generate a uniform random variable
Ubetween 0 and 1, thenwhich means that
F^-1(U)will have the same distribution asX!Of course this is only possible if you can invert the CDF, but in your case
Fis 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
then for each generated uniform number between 0 and 1, simply perform a dichotomy on the CDF table to retriver the corresponding index.