0
$\begingroup$

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 ?

$\endgroup$
3
  • $\begingroup$ What is your question? $\endgroup$
    – Evil
    Mar 8 '18 at 15:49
  • $\begingroup$ @Evil I don't know how to proceed at all. $\endgroup$ Mar 8 '18 at 15:51
  • $\begingroup$ 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. $\endgroup$
    – Evil
    Mar 8 '18 at 15:55
0
$\begingroup$

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} $

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

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$.

$\endgroup$
1
  • $\begingroup$ I changed my answer a little bit. $\endgroup$
    – kntgu
    Mar 8 '18 at 16:41
-1
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ Um. Unless your random number generator returns values to exactly two decimal places, your algorithm has a 34% chance of returning tails. $\endgroup$ Mar 8 '18 at 16:11
  • $\begingroup$ Please see my edit. $\endgroup$
    – josephmcm
    Mar 9 '18 at 14:23
  • $\begingroup$ 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. $\endgroup$ Mar 9 '18 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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