Relative probabilities using random number gen

Posted by CyberShot on Stack Overflow See other posts from Stack Overflow or by CyberShot
Published on 2012-06-09T04:34:39Z Indexed on 2012/06/09 4:40 UTC
Read the original article Hit count: 144

Filed under:
|

If I have relative probabilities of events A, B, C occurring.

i.e P(A) = 0.45, P(B) = 0.35, P(C) = 0.20,

How do I do represent this using a random number generator between 0 and 1?

i.e. R = rand(0,1)

if (R < 0.45)
   event A

else if(R < 0.35)
  event B

else if(R < 0.20)
  event C

The above works for two events A,B but I think the above is wrong for three or more since there is overlapping.

This is obviously a very simple question and the answer should be immediately evident, but I'm just too stupid to see it.

© Stack Overflow or respective owner

Related posts about random

Related posts about probability