Generate a Random Boolean Value in Python

In many situations, we need to generate random boolean values in Python. It is similar to flipping a coin to get either heads or tails. We need random values to generate test cases or get unbiased results. Therefore, in this tutorial, we will learn to generate random boolean values with Python using four different methods. We make use of the random module in Python which is an in-built module to generate random values.

Method 1: Using the getrandbits() function of the random module

The getrandbits() function of the random module in Python is used to return an integer value. The number of bits that the integer value represents is specified in the parameters. For instance, if the value 4 is passed in the parameter, then it can return an integer like 15  whose binary representation is 1111. Thus, in our case, we will pass 1 in the parameter. So the output will be either 1 or 0.  We then shall use the bool() function which will return the boolean value for the corresponding bit. If the value is 1 it will return True and for 0 it will return False.

import random

randBits = bool(random.getrandbits(1))

print(randBits)

Output:

Random boolean value generated is: True

Method 2: Using the randint() function of the random module

The randint() function in Python will return random integer values from the specified range given in the parameters. For instance, if the range given in the parameters of the function is (1,4), then it will return any random integer value between 1 and 4 (including 1 and 4). In this case, we pass the range as (0,1). Thus the function will return either 0 or 1 as an output. We will then use the bool() function to get the corresponding boolean value for the random integer generated.

import random

random_output = bool(random.randint(0,1))

print("Random boolean value generated is:",random_output)

Output:

Random boolean value generated is: True

Method 3: Using the choice() function of the random module

The choice() function of the random module in Python is used to generate a random value from the specified iterable passed in the parameters. The iterable can be a list, tuples, arrays, or strings. In this case, we pass a list with values as True and False. Thus the function returns a random boolean value from the tuple as an output.

import random

random_output = bool(random.choice([True, False]))

print("Random boolean value generated is:",random_output)

Output:

Random boolean value generated is: False

Method 4: Using the random() function of random module

The random() function in Python returns a random value from the range 0 to 1. So the logic used in the below-mentioned code is to check if the random value generated by the function is greater than 0.5. If it is greater than 0.5 it will return output as True, else False. Thus we can also generate a random boolean value using the random() function.

import random

random_output=random.random()>0.5

print("Random boolean value generated is:",random_output)

Output:

Random boolean value generated is: False

Thus we have reached the end of this tutorial on how to generate a random boolean value with Python. To read more about generating random values in Python click on the following link: How to generate a random letter in Python

Leave a Reply

Your email address will not be published. Required fields are marked *