# Write algorithm for biased toss of a coin

Given a biased coin whose probability for Heads is 0.67 and Tails is 0.33, write an algorithm which will print the Heads and Tails with this probability.

I am not able to proceed with the problem. What should be my approach ?

– Evil
Mar 8, 2018 at 15:49
• @Evil I don't know how to proceed at all. Mar 8, 2018 at 15:51
• Do you have some (pseudo)random number generator? Use it, the rest of this task is if/else with print, which is hardly an algorithm.
– Evil
Mar 8, 2018 at 15:55

You should just use a random number generator which is generally located in Math libraries. (Pseudo) Random number generators generally use linear congruences like $X_{i+1} = (a X_i + c) ~ {\rm mod} ~ m, ~~~ i = 0, 1, 2, ...$

function()

var rand := generateRandomNumber [1, 100]
if rand<=67 then
else
print "tails"
endif

end function


generateRandomNumber function will return a pseudorandom number between 1 and 100. So if we say your random variable is $X \\$
$Then,\; P(X\leq67)\; will\; be \; \frac{67}{100}\;and \; P(X>67)\; will\; be \; \frac{33} {100}$

• You should explain why this works. Mar 8, 2018 at 16:12
• @kntgu Shouldn't be the probability of generating heads will be 68 right ? I mean from [0,67] (both inclusive) ? Mar 8, 2018 at 16:50
• generateRandomNumber function generates an integer in the range [1,100] you can easily do that with a C expression rand_number = rand() % 100 + 1; Mar 8, 2018 at 16:53

Let $\xi$ be a random variable uniformly distribuited in $[0,1]$ and $t$ a real value in $(0,1)$, consider the function

$$Y(\xi;t) = \begin{cases} 1 & \xi < t \\ 0 & \text{otherwise} \end{cases}$$

Which is a discrete random variable therefore

$$p_Y(y) = (1-t) \delta(y) + t \delta(y - 1)$$

This means that if pick a random number in $[0,1]$ and you later compute $Y$, this $Y$ will correspond to a randomly picked value in $\left\{0,1\right\}$, but biased accordingly to $t$. If you use @kngtu pseudocode, it's equivalent to define the distribution above where $t = 0.67$.

• I changed my answer a little bit. Mar 8, 2018 at 16:41

If you can generate a random number between 0 and 1, then do

Edit: round random to 2 decimal places

if (random < 0.34) return "tails"; else return "heads";

That way (assuming the random number generator is actually random) you have a 33% change of tails and 67% chance of heads.

• Um. Unless your random number generator returns values to exactly two decimal places, your algorithm has a 34% chance of returning tails. Mar 8, 2018 at 16:11
• Please see my edit. Mar 9, 2018 at 14:23
• The more logical edit would be to just replace "$<0.34$" with "$\leq 0.33$" and then you don't need to make any assumptions about rounding. Mar 9, 2018 at 14:54