Skip to main content

How to Generate Random Booleans in Python

Generating random boolean values (True or False) is a common requirement in simulations, testing, and various algorithms.

This guide explores multiple methods for generating random booleans in Python, covering the use of the random module, weighted probabilities, and NumPy for generating arrays of random booleans.

Generating Random Booleans with random.getrandbits()

The random.getrandbits() method, combined with bool(), is a concise and efficient way to generate a random boolean:

import random

random_bool = bool(random.getrandbits(1))
print(random_bool) # Output: True or False
  • random.getrandbits(1) generates a random integer with 1 bit (either 0 or 1).
  • bool() converts 0 to False and 1 to True.

Generating Random Booleans with random.choice()

The random.choice() method can be used to randomly select from a list of True and False:

import random

random_bool = random.choice([True, False])
print(random_bool) # Output: True or False

Generating Random Booleans with random.random()

The random.random() function generates a random float between 0.0 (inclusive) and 1.0 (exclusive). You can compare this float to a threshold (e.g., 0.5) to get a boolean:

import random

random_bool = random.random() > 0.5
print(random_bool) # Output: True or False
  • This generates a boolean with a 50% chance of being True and a 50% chance of being False.

Generating Random Booleans with random.randint()

Similar to using random.random(), you can generate a random boolean by generating a random integer between 0 and 1 (inclusive), and convert it to a boolean:

import random

random_bool = bool(random.randint(0, 1))
print(random_bool) # Output: True or False
  • The random.randint(0, 1) returns a random integer between 0 and 1 (inclusive), and the bool() function converts the 0 to False and 1 to True.

Generating Random Booleans with Weighted Probabilities

To generate random booleans with a specific probability, use random.random() and compare it to your desired probability:

import random

def bool_based_on_probability(probability=0.5):
return random.random() < probability

# 75% probability of True
result = bool_based_on_probability(0.75)
print(result) # Output: True (most likely, but could be False)

# 25% probability of True
result = bool_based_on_probability(0.25)
print(result) # Output: False (most likely, but could be True)

Generating Lists of Random Booleans

Using random.choices()

The random.choices() function generates a list of random choices from a population with replacement:

import random

random_boolean_list = random.choices([True, False], k=3) # k defines the size of the generated list
print(random_boolean_list) # Output: (e.g., [True, True, False])

With Weighted Probabilities

You can also specify weights to control the probability of True vs. False:

import random

random_boolean_list = random.choices(
[True, False],
k=3,
weights=[75, 25] # 75% chance of True, 25% chance of False
)

print(random_boolean_list) # Output: (e.g., [True, True, False])

Generating Random Booleans with NumPy

NumPy provides efficient ways to generate arrays of random booleans. Install NumPy with pip install numpy.

Using numpy.random.rand()

Generate random floats and compare to a threshold:

import numpy as np

bool_array = np.random.rand() > 0.5
print(bool_array) # Output: True or False
  • The numpy random rand function generates floating point numbers between 0 and 1, and the comparison converts them to booleans.

Using numpy.random.randint()

Generate random integers (0 or 1) and then convert to booleans:

import numpy as np

bool_array = np.random.randint(2, size=8, dtype=bool) # Directly create boolean array.
print(bool_array) # Output: (e.g., [ True True False False True False True False])
  • The numpy random method randint returns a sequence of integers between 0 and 1 (the 2 parameter determines the maximum number, which is exclusive).
  • dtype=bool specifies the data type of the resulting array to be boolean. This is the most direct and efficient way to create a NumPy array of booleans.
  • The size argument determines the size of the output array.