There are several ways to generate a random boolean in Python, here are two easy options using the random module:
main.py75 chars5 lines
The randint
function generates a random integer between the two arguments (inclusive). We then use the bool()
function to convert the integer to a boolean value, where 0 is False and 1 is True.
main.py77 chars5 lines
The choice
function randomly selects an element from a given sequence. We pass in the sequence [True, False]
and it randomly selects either True
or False
.
gistlibby LogSnag