You are given a function foo() that represents a biased coin. When foo() is called, it returns 0 with 60% probability, and 1 with 40% probability. Write a new function that returns 0 and 1 with a 50% probability each. Your function should use only foo(), no other library method.

**Solution:**

We know foo() returns 0 with 60% probability. How can we ensure that 0 and 1 are returned with a 50% probability?

The solution is similar to this post. If we can somehow get two cases with equal probability, then we are done. We call foo() two times. Both calls will return 0 with a 60% probability. So the two pairs (0, 1) and (1, 0) will be generated with equal probability from two calls of foo(). Let us see how.**(0, 1):** The probability to get 0 followed by 1 from two calls of foo() = 0.6 * 0.4 = 0.24 **(1, 0):** The probability to get 1 followed by 0 from two calls of foo() = 0.4 * 0.6 = 0.24*So the two cases appear with equal probability. The idea is to return consider only the above two cases, return 0 in one case, return 1 in other case. For other cases [(0, 0) and (1, 1)], recur until you end up in any of the above two cases. *

The below program depicts how we can use foo() to return 0 and 1 with equal probability.

## C++

`#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `foo() ` `// given method that returns 0` ` ` `// with 60% probability and 1 with 40%` `{` ` ` `// some code here` `}` `// returns both 0 and 1 with 50% probability` `int` `my_fun()` `{` ` ` `int` `val1 = foo();` ` ` `int` `val2 = foo();` ` ` `if` `(val1 == 0 && val2 == 1)` ` ` `return` `0; ` `// Will reach here with` ` ` `// 0.24 probability` ` ` `if` `(val1 == 1 && val2 == 0)` ` ` `return` `1; ` `// Will reach here with` ` ` `// 0.24 probability` ` ` `return` `my_fun(); ` `// will reach here with` ` ` `// (1 - 0.24 - 0.24) probability` `}` `// Driver Code` `int` `main()` `{` ` ` `cout << my_fun();` ` ` `return` `0;` `}` `// This is code is contributed` `// by rathbhupendra` |

*chevron_right*

*filter_none*

## C

`#include <stdio.h>` `int` `foo() ` `// given method that returns 0 with 60%` ` ` `// probability and 1 with 40%` `{` ` ` `// some code here` `}` `// returns both 0 and 1 with 50% probability` `int` `my_fun()` `{` ` ` `int` `val1 = foo();` ` ` `int` `val2 = foo();` ` ` `if` `(val1 == 0 && val2 == 1)` ` ` `return` `0; ` `// Will reach here with 0.24 probability` ` ` `if` `(val1 == 1 && val2 == 0)` ` ` `return` `1; ` `// // Will reach here with 0.24` ` ` `// probability` ` ` `return` `my_fun(); ` `// will reach here with (1 - 0.24 -` ` ` `// 0.24) probability` `}` `int` `main()` `{` ` ` `printf` `(` `"%d "` `, my_fun());` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`import` `java.io.*;` `class` `GFG {` ` ` `// Given method that returns 0` ` ` `// with 60% probability and 1 with 40%` ` ` `static` `int` `foo()` ` ` `{` ` ` `// some code here` ` ` `}` ` ` `// Returns both 0 and 1 with 50% probability` ` ` `static` `int` `my_fun()` ` ` `{` ` ` `int` `val1 = foo();` ` ` `int` `val2 = foo();` ` ` `if` `(val1 == ` `0` `&& val2 == ` `1` `)` ` ` `return` `0` `; ` `// Will reach here with` ` ` `// 0.24 probability` ` ` `if` `(val1 == ` `1` `&& val2 == ` `0` `)` ` ` `return` `1` `; ` `// Will reach here with` ` ` `// 0.24 probability` ` ` `return` `my_fun(); ` `// will reach here with` ` ` `// (1 - 0.24 - 0.24) probability` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `System.out.println(my_fun());` ` ` `}` `}` `// This code is contributed by ShubhamCoder` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the ` `# above approach` `def` `foo():` ` ` ` ` `# Some code here` ` ` `pass` `# Returns both 0 and 1 ` `# with 50% probability` `def` `my_fun():` ` ` ` ` `val1, val2 ` `=` `foo(), foo()` ` ` ` ` `if` `val1 ^ val2:` ` ` ` ` `# Will reach here with ` ` ` `# (0.24 + 0.24) probability` ` ` `return` `val1` ` ` ` ` `# Will reach here with ` ` ` `# (1 - 0.24 - 0.24) probability` ` ` `return` `my_fun()` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `print` `(my_fun())` `# This code is contributed by sgshah2` |

*chevron_right*

*filter_none*

## C#

`using` `System;` `class` `GFG {` ` ` `// given method that returns 0` ` ` `// with 60% probability and 1 with 40%` ` ` `static` `int` `foo()` ` ` `{` ` ` `// some code here` ` ` `}` ` ` `// returns both 0 and 1 with 50% probability` ` ` `static` `int` `my_fun()` ` ` `{` ` ` `int` `val1 = foo();` ` ` `int` `val2 = foo();` ` ` `if` `(val1 == 0 && val2 == 1)` ` ` `return` `0; ` `// Will reach here with` ` ` `// 0.24 probability` ` ` `if` `(val1 == 1 && val2 == 0)` ` ` `return` `1; ` `// Will reach here with` ` ` `// 0.24 probability` ` ` `return` `my_fun(); ` `// will reach here with` ` ` `// (1 - 0.24 - 0.24) probability` ` ` `}` ` ` `// Driver Code` ` ` `static` `public` `void` `Main() { Console.Write(my_fun()); }` `}` `// This is code is contributed` `// by ShubhamCoder` |

*chevron_right*

*filter_none*

## PHP

`<?php` `function` `foo() ` `// given method that returns 0 ` ` ` `// with 60% probability and 1 with 40% ` `{ ` ` ` `// some code here ` `} ` `// returns both 0 and 1 with 50% probability ` `function` `my_fun() ` `{ ` ` ` `$val1` `= foo(); ` ` ` `$val2` `= foo(); ` ` ` `if` `(` `$val1` `== 0 && ` `$val2` `== 1) ` ` ` `return` `0; ` `// Will reach here with` ` ` `// 0.24 probability ` ` ` `if` `(` `$val1` `== 1 && ` `$val2` `== 0) ` ` ` `return` `1; ` `// Will reach here with` ` ` `// 0.24 probability ` ` ` `return` `my_fun(); ` `// will reach here with` ` ` `// (1 - 0.24 - 0.24) probability ` `} ` `// Driver Code` `echo` `my_fun(); ` `// This is code is contributed ` `// by Akanksha Rai` `?>` |

*chevron_right*

*filter_none*

References:

http://en.wikipedia.org/wiki/Fair_coin#Fair_results_from_a_biased_coin

This article is compiled by **Shashank Sinha** and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Coin Change | DP-7
- Frobenius coin problem
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Expected number of coin flips to get two heads in a row?
- Coin Change | BFS Approach
- Find the player who will win the Coin game
- Probability of getting K heads in N coin tosses
- Count of total Heads and Tails after N flips in a coin
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Philaland Coin | TCS Mockvita 2020
- Count ways to distribute exactly one coin to each worker
- Probability of not getting two consecutive heads together in N tosses of coin
- Find minimum number to be divided to make a number a perfect square
- Find minimum number of coins that make a given value
- Find minimum number of merge operations to make an array palindrome
- Removing a number from array to make it Geometric Progression
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Print digit's position to be removed to make a number divisible by 6
- Minimum gcd operations to make all array elements one
- Minimum flip required to make Binary Matrix symmetric