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 ?
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It only takes a minute to sign up.
Sign up to join this communityGiven 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 ?
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
print "heads"
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} $
rand_number = rand() % 100 + 1;
$\endgroup$
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$.
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.