# Get a Random Boolean in Python

If you’re in a hurry, and you need 50/50 chances of getting either value, just copy this:

`import random random_boolean = bool(random.getrandbits(1))`

However, if you want to know more about generating random booleans, or if you need more advanced cases like being cryptographically secure or generating booleans with different degrees of probability, keep reading!

Generating random boolean values can be useful in a variety of applications, such as simulations, testing, and decision-making algorithms. By generating random boolean values, you can introduce variability and uncertainty into your code, which can be helpful in creating more robust and adaptable software systems. Additionally, generating random boolean values with different probabilities can be useful in creating more realistic simulations or in modeling complex systems where outcomes aren’t always certain.

In this article, we will explore different ways to generate random boolean values using the `random`

and `secrets`

modules in Python. We will cover the basics of generating random boolean values and then dive into more advanced techniques, such as generating random boolean values with different degrees of probability for each value.

By the end of this article, you will have the knowledge and tools to generate random boolean values in Python for your specific needs. Whether you’re a beginner or an experienced developer looking to expand your Python skills, this article will provide you with valuable insights and techniques that you can use in your projects.

So let’s get started and learn how to get a random boolean with Python!

## Generating Random Booleans in Python with the `random`

Module

Because this is all very straightforward stuff, let’s jump directly into the juice!

### Using `random.getrandbits()`

The `random.getrandbits()`

method in Python generates a specified number of random bits, which can then be used to generate a random boolean. The method takes an integer argument that specifies the number of bits to generate.

To generate a random boolean using `random.getrandbits()`

, you can generate a 1-bit integer value using the method, and then convert the integer value to a boolean value using the `bool()`

function.

Here is an example code snippet to generate a random boolean using `random.getrandbits()`

:

```
import random
# Generating a random 1-bit integer value using random.getrandbits() method
random_bit = random.getrandbits(1)
# Converting the integer value to a boolean value
random_boolean = bool(random_bit)
print(random_boolean)
```

### Using `random.choice()`

The `random.choice()`

method in Python is used to generate a random element from a given sequence, such as a list or a tuple. In the context of generating random booleans, you can use `random.choice()`

to randomly select between `True`

and `False`

.

Here is an example code snippet to generate a random boolean using `random.choice()`

:

```
import random
# Generating a random boolean value using random.choice() method
random_boolean = random.choice([True, False])
print(random_boolean)
```

### using `random.randint()`

The `random.randint(a, b)`

method in Python generates a random integer value between `a`

and `b`

, inclusive. To use this method to generate a random boolean, you can set `a`

to 0 and `b`

to 1, which will generate an integer value of either 0 or 1. Then, you can convert this integer value to a boolean value using the `bool()`

function.

Here is an example code snippet to generate a random boolean using `random.randint()`

:

```
import random
# Generating a random integer value in the range [0, 1] using random.randint() method
random_integer = random.randint(0, 1)
# Converting the integer value to a boolean value
random_boolean = bool(random_integer)
print(random_boolean)
```

This method is less efficient than the other methods described in the article, but it is still a valid way to generate random booleans in Python.

## Generating Random Booleans in Python with the `secrets`

Module

In cryptography, a cryptographically secure algorithm is one that is designed to be practically impossible to break. This means that even if an attacker has access to the algorithm and its inputs, they shouldn’t be able to derive the output or any sensitive information from the output.

The `secrets`

module is designed to generate cryptographically secure random numbers, which are suitable for generating random values that need to be unpredictable and secure. The `random`

module, on the other hand, generates pseudo-random numbers that aren’t suitable for cryptographic purposes. If you need to generate random values for security-related applications, such as password generation or encryption, then you should use the `secrets`

module. If security is not a concern, then the `random`

module is generally faster and more efficient for generating random values.

Here is an example code snippet to generate a random boolean using `secrets.choice()`

:

```
import secrets
# Generating a random boolean value using secrets.choice() method
random_boolean = secrets.choice([True, False])
print(random_boolean)
```

By using the `secrets`

module, you can be confident that the random values you generate are secure and unpredictable. However, it is important to note that generating cryptographically secure random values can be slower and less efficient than generating non-secure random values using the `random`

module. If you don’t require high levels of security for your random values, it is generally safe to use the `random`

module instead of the `secrets`

module.

## Generate Random Booleans with Different Degrees of Probabilities for Each Value

So far, we’ve explored different ways to generate random boolean values with a 50% chance for each value. However, sometimes we may want other options, like a 75% chance of getting True.

Let’s explore now how to generate random boolean values with different degrees of probabilities for each value using the `random`

module in Python:

```
import random
# Generating a random float value between 0 and 1 using random.random() method
random_float = random.random()
# Setting the probability of a True boolean value to 0.75
true_probability = 0.75
# Generating a random boolean value with a 75% chance of being True
random_boolean = random_float < true_probability
print(random_boolean)
```

The code above generates a random boolean value with a 75% chance of being True. It does this by generating a random float value between 0 and 1 using `random.random()`

method. Then, it sets the probability of a True boolean value to 0.75. Finally, it generates a random boolean value with a 75% chance of being True by comparing the random float value to the true probability value using the less than operator. If the random float value is less than the true probability value, the generated boolean value will be True. Otherwise, the generated boolean value will be False.

## FAQs

If you liked what you saw, please support my work!

Juan Cruz Martinez

Juan has made it his mission to help aspiring developers unlock their full potential. With over two decades of hands-on programming experience, he understands the challenges and rewards of learning to code. By providing accessible and engaging educational content, Juan has cultivated a community of learners who share their passion for coding. Leveraging his expertise and empathetic teaching approach, Juan has successfully guided countless students on their journey to becoming skilled developers, transforming lives through the power of technology.