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,
1 2 3 4 5 6 |
import random random_bits = random.getrandbits(1) random_bool = bool(random_bits) print(random_bool) |
Output:
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,
1 2 3 4 5 |
import random a = bool(random.choice([True, False])) print(a) |
Output:
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,
1 2 3 4 5 |
import random a = random.random() > 0.5 print(a) |
Output:
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,
1 2 3 4 5 |
import random a = bool(random.randint(0,1)) print(a) |
Output:
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,
1 2 3 4 5 6 7 |
import numpy as np initial_arr = [True, False] arr = np.random.choice(initial_arr, size=5) bool_arr = list(map(bool, arr)) print(bool_arr) |
Output:
In the above example,
- We generate an array of random Boolean values having a total of five elements.
- The
map()
function applies thebool()
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,
1 2 3 4 5 |
import numpy as np arr = np.random.rand(5) > 0.5 print(arr) |
Output:
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,
1 2 3 4 5 6 |
import numpy as np arr = np.random.randint(2,size = 5) bool_arr = list(map(bool,arr)) print(bool_arr) |
Output:
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,
1 2 3 4 5 |
from faker import Faker fake = Faker() print(fake.pybool()) |
Output:
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.