How to get a random boolean in Python

A random boolean value has equal probability of being either True or False.

Solution for How to get a random boolean in Python : You can use random.getrandbits() to get a random boolean value Call random.getrandbits(k) with k set to 1 to get a single random bit. A bit is either 0 (representing False) or 1 (representing True). Use bool() to convert the random bit into a bool.


how-to-get-a-random-boolean-in-python