Python

Python Generate a Random Boolean Value

Are you looking for an elegant and fast way to generate a random Boolean value in Python? You have come to the right place, we have a perfect solution for you. This article was designed to make you learn how to generate a random Boolean value in Python in the best way possible. Generating a random number is just like flipping a coin. Here, we will define some easy ways to generate a random Boolean value in Python with the help of examples, but let us first have a brief introduction of random Boolean values.

In general, a random number is quite instrumental in programing languages. These numbers are used to generate test cases or stimulations. A Boolean value just has two options; True or False. The probability of being True or False is equal. The other value could be 0 or 1. However, the Boolean value always has only two options. In other words, 0 represents False and 1 represents True value. Python programing language provides a built-in Boolean data type that represents the False or True values. Python provides several ways to generate random Boolean values. This article will demonstrate the best way possible to generate a random Boolean value in Python with the help of examples.

Example 1:

This example is about generating a random Boolean value using the random.getrandbits(). The random.getrandbits() method in Python is the method of the random module. This function will return a number with the specified number of bits. The parameters or argument of the function represents the number of bits returned. Here is an example of random.getrandbits(). The following code will randomly generate True or False values. Every time you execute the above code, a different value, either True or False, will be generated.

import random

randBits = bool(random.getrandbits(1))

print(randBits)

Text Description automatically generated

When we execute the code, a True value is generated. See the output given below. Note that 1 is passed as a parameter of the random.getrandbits() function. The parameter 1 will generate either 1 or 0 Boolean values. Then bool() function is used to convert the integer value into a True or False Boolean value.

Example 2:

Here, we are going to use the random.choice() method for generating a random Boolean value. The random.choice() function randomly selects a value from a given list of values. In this example, we will create a list of 2 values, True and False, and then use the choice() function to generate a random value from them. See the example code given below:

import random

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

print(randBits)

Graphical user interface, text Description automatically generated with medium confidence

The output of the above code is again either True or False because of the bool function. See the output given below. The choice() function has randomly selected a value from the given list and returned it as output.

Example 3:

This example is slightly different from the above two. We will be using a random.random() function for generating a random Boolean value. The random() function generates a random floating number ranging from O to 1. By creating a customized function, we can generate the random Boolean value. See the following image to understand the functionality of the random() method.

import random

print(random.random() > 0)

Shape Description automatically generated with medium confidence

Note that the output of random.random() method is compared with 0. Here is what is happening in the program. The random.random() function generated a random floating number between 0 and 1. The generated number is compared with 0 by using the if() function. If the generated number is greater than 0, the used method will return True as output, else it will return False. Here is the final output of the above code.

Example 4:

Finally, we are going to use random.randint() function. The functionality of random.randint() is similar to the random.getrandbits() method. The random.randint() generated a random integer ranging between the given two numbers. The randint() takes two numbers as arguments and generates a random number between those two numbers. See the code below and understand what this function does.

import random

print(bool(random.randint(0,1)))

A screenshot of a computer Description automatically generated with medium confidence

Note that the bool() function is used to convert the output to a Boolean value. Here is what you get as output.

Conclusion:

This article is about how to generate a random Boolean value in Python. Python is the most advanced high-level language which provides a simple and easy built-in function to get a random Boolean number. The randomly generated Boolean value is always either True or False, or it could be 0 or 1. There are several ways to generate a random Boolean value in Python. In this article, we have discussed the most commonly used four methods and demonstrated their functionality with the help of examples. Different methods involve random.getrandbits(), random.random(), random.choice(), and random.randint() methods were discussed. The function of these methods is relatively the same; however, they all are used to generate the same output. The bool() function is used to convert the output into a Boolean value representing True or False.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content