Last updated: Apr 10, 2024
Reading timeยท4 min
To generate a random boolean value:
random.getrandbits()
method to get an integer with 1 random bit.bool()
class to convert the integer to a boolean value.import random result = bool(random.getrandbits(1)) print(result) # ๐๏ธ False result = bool(random.getrandbits(1)) print(result) # ๐๏ธ True
The
random.getrandbits()
method returns a non-negative Python integer with k
random bits.
We set the k
argument to 1
, so the only possible values the method returns
are 0
and 1
.
import random print(random.getrandbits(1)) # ๐๏ธ 1 print(random.getrandbits(1)) # ๐๏ธ 0 print(random.getrandbits(1)) # ๐๏ธ 0
The last step is to use the bool() class to convert the integer to a boolean.
import random result = bool(random.getrandbits(1)) print(result) # ๐๏ธ False result = bool(random.getrandbits(1)) print(result) # ๐๏ธ True
Converting 0
to a boolean returns False
and converting 1
to a boolean
returns True
.
print(bool(0)) # ๐๏ธ False print(bool(1)) # ๐๏ธ True
You can also use the random.choice()
method to generate a random boolean, but
the method is a bit less performant.
import random random_bool = random.choice([True, False]) print(random_bool) # ๐๏ธ False
The random.choice() method takes a sequence and returns a random element from the non-empty sequence.
You can also use the random.random()
method to generate a random boolean.
import random random_bool = random.random() > 0.5 print(random_bool)
The
random.random()
number returns the next random floating-point number in the range [0.0,1.0)
.
The condition returns the result of checking if the generated number is greater
than 0.5
.
You can also use the random.randint()
method to generate random boolean
values.
import random random_bool = bool(random.randint(0, 1)) print(random_bool) # ๐๏ธ True
The
random.randint()
function takes 2 numbers - a
and b
as parameters and returns a random
integer in the range.
Note that the range is inclusive - meaning both a
and b
can be returned.
If you need to generate a random boolean value based on probability, use the
random.random()
method.
random.random()
method to get a random float from 0
to 1
.random.random() < 0.75
returns True
75% of the time.import random def bool_based_on_probability(probability=0.5): return random.random() < probability # ๐๏ธ 75% probability to return True result = bool_based_on_probability(0.75) print(result) # ๐๏ธ True # ๐๏ธ 25% probability to return True result = bool_based_on_probability(0.25) print(result) # ๐๏ธ False
The
random.random()
number returns the next random floating-point number in the range [0.0,1.0)
.
The method could return 0
, but it could never return 1
.
Comparing the two floating-point numbers returns a boolean value.
For example, if you set the probability
argument to 0.75
, the function has a
75%
chance to return True
.
If you set the argument to 1
, the function will always return True
.
Use the random.choices()
method to generate a list of multiple random boolean
values.
The random.choices()
method returns a k
sized list of elements chosen from
the iterable.
import random random_boolean_list = random.choices([True, False], k=3) print(random_boolean_list) # ๐๏ธ [True, True, False] # ๐๏ธ with probability (specify weights) random_boolean_list = random.choices( [True, False], k=3, weights=[75, 25] ) print(random_boolean_list) # ๐๏ธ [True, True, False]
The
random.choices()
method returns a k
sized list of elements chosen from the provided iterable
with replacement.
import random a_list = ['bobby', 'hadz', '.', 'com'] result = random.choices(a_list, k=3) print(result) # ๐๏ธ ['hadz', 'hadz', 'bobby']
If you need to return the boolean values based on probability, set the weights
argument in the call to random.choices()
.
import random random_boolean_list = random.choices( [True, False], k=3, weights=[75, 25] ) print(random_boolean_list) # ๐๏ธ [True, True, False]
The call to the method has a 75% chance to return a True
value and a 25%
chance to return False
.
You can also use the numpy.random.rand()
method to generate a random boolean
value.
import numpy as np bool_array = np.random.rand() > 0.5 print(bool_array) # ๐๏ธ False
Make sure you have the NumPy module installed to be able to run the code sample.
pip install numpy pip3 install numpy
The
numpy.random.rand()
method returns a single Python float from 0
to 1
.
import numpy as np print(np.random.rand()) # ๐๏ธ 0.400755652315017
We compare the float to 0.5
to get a boolean result.
You can also use the random.randint()
method from the NumPy module to generate
an array of random booleans.
import numpy as np bool_array = np.random.randint(2, size=8) > 0.5 # [ True True False False True False True False] print(bool_array)
Make sure you have the NumPy module installed to be able to run the code sample.
pip install numpy pip3 install numpy
The numpy.random.randint() method returns random integers from low (inclusive) to high (exclusive).
import numpy as np bool_array = np.random.randint(2, size=8) # [0 0 1 1 1 0 1 0] print(bool_array)
We simply compare the array to 0.5
to get an array of random boolean values.
import numpy as np bool_array = np.random.randint(2, size=8) > 0.5 # [ True True False False True False True False] print(bool_array)
You can learn more about the related topics by checking out the following tutorials: