Write an algorithm to get 0 and 1 with equal probability using a function that generates random numbers from 1 to 5 with equal probability.

Practice this problem

The algorithm can be implemented as follows in C (self-explanatory):

Download  Run Code

Output (will vary):

0 ~ 50.23%
1 ~ 49.77%

 
We can also do something like below, but this will increase the number of calls made to the random() function: