In Python, we can easily get a random boolean using the Python random() or choice() function from the random module.

import random import choice, random

#Using random.random() 
random_bool_with_random = True if random() > 0.5 else False

#Using random.choice()
random_bool_with_choice = choice([True, False])

print(random_bool_with_random)
print(random_bool_with_choice)

#Output:
True
False

Being able to generate random numbers efficiently when working with a programming language is very important. In Python, we can generate random number easily to get random boolean values.

To get a boolean randomly, we can use the Python random module. In the Python random module, we can use the Python random() function, or Python choice() function.

The random() function generates a random float between 0 and 1. The Python choice() function takes in a list of choices and gives a random selection from those choices.

Below is an example of how to get a boolean values randomly in Python.

import random import choice, random

#Using random.random() 
random_bool_with_random = True if random() > 0.5 else False

#Using random.choice()
random_bool_with_choice = choice([True, False])

print(random_bool_with_random)
print(random_bool_with_choice)

#Output:
True
False

One such application of generating random boolean values would be if you wanted to generate a coin flip in Python.

Below is some sample code of how you could flip a coin in Python using the random module.

from random import choice, random

#Using random.choice()
coin_flip_with_choice = choice(["Heads","Tails"])

#Using random.random() 
coin_flip_with_random = "Heads" if random() > 0.5 else "Tails"


print(coin_flip_with_choice)
print(coin_flip_with_random)

#Output:
Tails
Heads

Using Python to Generate a List of Random Booleans in a Loop

If you want to generate a list of random booleans, we can easily define a function and use a loop in Python.

In this example, we will create a function which takes one argument, the number of booleans you want to create, and will return a list of random booleans.

Below is some sample code which will get the random booleans for you in Python.

from random import random

def rand_bools(n):
    bools = []
    for x in range(0,n):
        bools.append(True if random() > 0.5 else False)
    return bools

print(rand_bools(10))

#Output:
[True, True, True, True, True, False, True, True, True, False]

Generating a Random Boolean With Probability

In the examples above, we’ve been assuming that we want to have 50% True and 50% False generated from our Python program.

If we want to creat a random boolean based on a probability, we can use the random() function and adjust the threshold.

For example, if we want to generate True 70% of the time, then we would do the following in Python:

import random import choice, random

#Using random.random() 
random_bool_with_random = True if random() > 0.7 else False

Generating Random Booleans with numpy and pandas in Python

If you are using numpy or pandas, we can fill a column with random boolean values using the numpy random.rand() allows us to generate random numbers in the same way as the Python random module.

Below is some code which will allow you to get boolean values randomly in Python with numpy.

import pandas as pd
import numpy as np

coin_flip_series = pd.Series(np.random.randint(2, size=10))

print(coin_flip_series)

#Output:
0    1
1    0
2    0
3    0
4    1
5    1
6    1
7    0
8    0
9    1
dtype: int32

Hopefully this article has been beneficial for you to learn how to get a random boolean in Python using the random module.

Categorized in:

Python,

Last Update: March 20, 2024