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.
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.
randBits = bool(random.getrandbits(1))
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.
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:
randBits = bool(random.choice([True, False]))
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.
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.
print(random.random() > 0)
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.
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.
Note that the bool() function is used to convert the output to a Boolean value. Here is what you get as output.
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.