Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am allowed to make any number of coin tosses. Coin is unbiased. How can I generate a given random variable X (say X is bernouille or binomial or uniform) using independent coin tosses. I am looking at this direction. Given X a random variable, I have to conduct an m coin tosses experiment for some m such that we associate X=i to some event say E_i in the experiment such that probability of occurrence of the event E_i is same as that of p(X=i) for each possible value i that X takes.

Can you help me how to generate random variable say bernouille (p)/binomial (n,p) at least using coin tosses.


share|cite|improve this question
up vote 4 down vote accepted

I assume that you want to generate an artificial sample of the random variable $X$. If you want a general method, you might use the inverse transform sampling: use your coin to generate a random number between $0$ and $1$ (for instance, count tails as 1s and heads as 0s and generate a binary number .11101001... to the desired accuracy), and then evaluate the quantile function (which is more or less the inverse of the cumulative distribution function) on this number.

For each concrete distribution there may be more effective methods, of course. But the above procedure is simple enough in the case of the Bernoulli distribution: toss your coin until you know whether the binary number .11101001... is greater or less than $1-p$ ($p$ being the probability of success). If it is less than $1-p$, count that as a failure. If it is greater than $1-p,$ count it as a success.

share|cite|improve this answer
Thanks for your answer. – TheNeo Apr 19 '12 at 8:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.