Get 0 and 1 with equal Probability using specified function

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


Below is the C implementation (self-explanatory) –


Download   Run Code

Output (will vary):

0 ~ 50.23%
1 ~ 49.77%


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



Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of