# Solved – Biased coin toss simulation — which random generator is most appropriate

I have a doubt whether the uniformly distributed random generator is the most appropriate to use for simulating biased coin toss.

https://stackoverflow.com/questions/477237/how-do-i-simulate-flip-of-biased-coin-in-python

Let's say a chance of Democrat winning in a state is 0.6. Ok, so I thought at first it makes sense to use uniform…, in infinite number of trials, in 60% occasions the value will be below 0.6 (democrat winning).

However I was also wondering, should I use normal distribution instead?

So let's assign the value (x) = 0 for democrat, and value 1 for republican. In the chart, we can draw a bar whose height is 0.6 on x(0), and another bar 0.4 on x(1).

That makes the expected value (mu) = 0x0.6 + 0x0.4 = 0.4
The std deviation would be: square root of pow(0 – 0.4,2)*0.6 + pow(1-0.4,2)*0.4 = sort(24/100) = 0.49

Now, I execute random.gauss on python:

``import random random.gauss(0.4, 0.49) ``

I got: 0.9193140340408493

What do I make of that value? Should I interpret it as a win for Republican (under the rule: anything to the right of mean value goes to republican, the rest to democrat) ?

If that's not true, please help me pointing out the flaw in my reasoning, and a pointer to a topic in stats book to fix that.

Raka

ADDITION: background: I want to implement spinner similar to the one I see in NYT site: http://www.nytimes.com/newsgraphics/2014/senate-model/ .

The reason I was wondering if we should use normal random is because when I saw the source code of that page, it also uses normal random. Here's a snippet of the source code from that page (it's the click event handler; function named 'click'):

``// Compute the national bias.   var nationalErrorScale = data.parameters.nationalErrorScale,       nationalBias = normalRandom() * nationalErrorScale,       localErrorScale = Math.sqrt(1 - nationalErrorScale * nationalErrorScale);    // For each race,   race.each(function(race) {      // Pick one of the potential matchups based on probabilities of each.     var matchup = race.matchups[bisectCumulative(race.matchups, Math.random())];      // Compute the local bias.     var localBias = normalRandom() * localErrorScale;      race.spinnerResult = matchup.mean + matchup.scale * (localBias + nationalBias) > 0 ? "dem" : "rep";   }); ``
Contents

You want to draw from Bernuli(p=0.4), but you draw from Normal(mu=0.4, sigma^2 = 0.24). It seems you are these to be the same because they have the same mean and variance. That is not correct.

Here is a way to construct a Bernuli(p=0.4) from a Normal(mu=0.4, sigma^2 = 0.24): If A has distribution Normal(mu=0.4, sigma^2 = 0.24), then
P(A<= a) = phi( a-0.4)/sqrt(0.24)),
so if a is chosen as a = 0.4 + sqrt(0.24)phi_inverse(0.4) = 0.4 + 0.489897..(-0.2533471) = 0.2758858, then X = (A <= a) satisfies:
P(X=1) = P(A<=a) = 0.4

If A = 0.9193140340408493.., then the corresponding X is 0 (since 0.9193140340408493..>0.2758858..)

There are many other ways of constructing a Bernuli(p=0.4) from a Normal(mu=0.4, sigma^2 = 0.24).

Using a uniform random variable is simpler, because one does not need to use the cumulative distribution function of a normal random variable.

Rate this post