Get random boolean in Python

Get random boolean in Python

What are Boolean Values?

Python has a built-in data type Boolean that represents True and False values. It has only these two values. We can also consider True and False as 1 and 0, respectively. If we convert 0 or 1 to Boolean, then we get True or False only.

This tutorial will demonstrate how to generate random Boolean values in Python.

Ways to Generate Random Boolean in Python

Random numbers are very instrumental in programming. We tend to use them for generating test cases or simulations. There are several methods associated with random number generation in Python. We will discuss how to generate random Boolean values.

Using the random.getrandbits() function

The getrandbits method of the random module returns the integers or numbers with a specified number of bits. The number of bits returned is placed as an argument or a parameter of this function.

For example,

Output:

True

In the above example,

  • We pass one as the parameter of the getrandbits() function. This will generate either 0 or 1.
  • Then, we use the bool() function to convert this randomly generated integer to a Boolean value, i.e., True or False and displayed it.

Using the random.choice() function

With the random.choice() function, we can randomly select an element from a given sequence like a list.

To generate random Boolean values using this function, we will create a list of two elements, True and False and then randomly select a value from this list.

For example,

Output:

True

We use the bool() function in the above example also. This function ensures that the final value is of type bool.

Using the random.random() function

The random.random() function generates a random float number between 0 and 1. We can use it to generate random Boolean values.

For example,

Output:

False

In the above code,

  • We generate a random float value using the random.random() function.
  • Then, we compare this value with 0.5. If the value is greater than 0.5, it will return True, or it will return False.
  • This way, by comparing it with 0.5, we get a random Boolean value.

Using the random.randint() function

This method is similar to the previously discussed random.getrandbits() method.

We will use the random.randint() function to get a random integer, 0 or 1. Then we convert this to Boolean using the bool() function.

For example,

Output:

True

Using the numpy.random.choice() function

The numpy module has a variety of random functions to generate random arrays of specific sizes. The numpy.random.choice() function returns a random set of samples in a one-dimensional array that returns a random numpy array. This method will create a random Boolean array in this method.

For example,

Output:

[ True True False True False]

In the above example,

  • We generate an array of random Boolean values having a total of five elements.
  • The map() function applies the bool() method to all the elements of the array.
  • The list() function converts the map object to a list.

Using the numpy.random.rand() function

The numpy.random.rand() function generates an array of the given size with random float values. We will follow a similar logic as discussed in the random.random() method.

For example,

Output:

[False False True True True]

Using the numpy.random.randint() function

This function will create an array of random integers between a given range.

We will generate an array of random integers, either 0 or 1, and convert them to Boolean values using the bool() function.

For example,

Output:

[ True True False True False]

Using the faker.pybool() function

The faker library can generate various types of fake test data. We first create an object using the Faker() constructor and the pybool function to get a random Boolean value.

For example,

Output:

True

Conclusion

In this article, we discussed how to generate random Boolean values in Python. Different methods involving the random, numpy, and faker() module were discussed. The functions of the numpy module are more hopeful in generating arrays of such values.

Was this post helpful?

Leave a Reply

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