I want to know how to model random variables using "basic operations". The only random function that I know, at least for C, is rand(), as well as srandfor sowing. There probably are packages somewhere on the Internet, but lets say that I want to implement it myself. I don't know if there are other very common random functions, but if not, let's just stick to rand()C.
rand()allows me to pseudo-randomly generate intfrom 0to RAND_MAX. Then I can use modto get intin some range. I can choose mod 2to select a sign and get negative numbers. I can also do rand()/RAND_MAXto simulate the values in the interval (0,1)and shift it to the model Uniform(a,b).
But I'm not sure if I can expand this to model any probability distribution and at what point I need to worry about accuracy, especially when it comes to infinities and irrational probabilities. In addition, this method is very rude, so I would like to learn more standard ways to use the basic tools, if any.
A simple example:
I have a random variable Xsuch as Pr(X = 1)=1/piand Pr(X=0)=1-1/pi. Since it piis irrational, I would approximate the probability of getting 1/piwith rand()and choose X=1if I get intfrom 0to Round(RAND_MAX*1/pi). Thus, it approaches twice, once for piand one more time for rounding.
Is there a better approach? How could one plan something more complex, for example, a continuous random variable on an interval (0,infinity)or a discrete random variable with irrational probabilities on a countably infinite set. Will my approach work, or will I have to worry about rounding errors?
EDIT: Also, how does pseudo-randomness change instead of randomness, rand()and how can I explain these changes?