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:



1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of