| by Arround The Web | No comments

Python Generate a Random Boolean Value

An RNG or Random Number Generator is something that is used by a lot of programs for various different reasons. However, oftentimes, the user wants to implement a boolean generator. To do this, the user can utilize the bool() method to convert the output of the random number generator into boolean values. This bool() method can be used alongside the getrandbits() method, the choice() method, and the random() function within a conditional statement.

This post will contain the following content:

Let’s start with the first method.

Method 1: Using the getrandbits() Method to Generate Boolean Values

The getrandbits() is used to generate bits, basically integer values, within a range specified in the argument of this method. With this method, the user can set the range to 1, which would mean that it can only generate either a 0 or a 1, and then use the bool() method to convert the integer to its boolean equivalent. However, to use this method, the user needs to import the “random” package.

To demonstrate this method for generating random boolean value, take the following code snippet:

import random
randInt = (random.getrandbits(1))
randBool = bool(randInt)
print(randBool)

 

When this code snippet is executed, it produces the following result on the terminal:

The above image contains the output of multiple executions of the code, confirming that a random boolean is generated on every execution.

Note: The getrandbits() method is the fastest method of this post to generate random boolean values.

Method 2: Using the choice() Method to Generate Boolean Values

The choice() from the “random” package is used to choose a random value from a list of values (strings, tuples, lists, and more). This method can be used to choose between True, and False by providing them in a list, or to choose between 0 and 1 and then converting it to a boolean using the bool() method.

To demonstrate the working of the choice() method to generate random boolean values, take a look at the following code snippet:

import random
randBool = random.choice([True, False])
print(randBool)

 

Upon execution, the following result is shown on the terminal:

The output verifies that a random boolean value was generated. Alternatively, for the second approach with the use of the choice() method, take a look at the below-given code snippet:

import random
randInt = random.choice([0,1])
randBool = bool(randInt)
print(randBool)

 

When this code snippet is executed, it displays the following result on the terminal:

The output is indeed a random boolean generated on each execution of the code.

Method 3: Using the random() Method With if-condition to Generate Boolean Values

The random() method can also be used to generate random boolean values. However, most of the users use it wrong. The users take the output of the random() method and then pass it to the bool() method to convert that value into a boolean. The output is indeed a boolean value, but they created a biased random boolean value generator using the wrong approach.

The random() method produces floating point values between 0 and 1, and users multiply it by 10 to get the value in integers. But, the values then range from 0 to 10. When this value is converted into a bool, it gives “False” for only “0”. This means that there is a 90% probability that the answer is going to be True. Thus, a biased random boolean generator.

To correctly, use the random() method to generate boolean values, apply an if-condition like this:

import random

randBool = random.random() > 0.5
print(randBool)

 

In this code snippet, the condition checks whether the value generated by the random() method is above 0.5 or not. Based on this, it returns True or False, achieving a 50% chance to land True or False. Upon execution, the following is the result:

Method 4: Using the randint() Method to Generate Boolean Values

The randint() method is used to generate a random integer value from a given range. If the provided range is only 0 and 1, and the output of this method is passed into the bool() method, then it will act as a random boolean generator:

import random
randInt = random.randint(0,1)
randBool = bool(randInt)
print(randBool)

 

When this code is executed it produces the following output on the terminal:

The output is a randomly generated boolean value for every execution of this code.

Conclusion

Generating Random Boolean Values can be helpful in quite a few tasks, especially when building an RNG system. To do this, the user can utilize various methods () in combination, such as the getrandbits(), choice(), random(), and the randint() method with the bool() method. This post has demonstrated the use of these methods.

Share Button

Source: linuxhint.com

Leave a Reply