python
boolean
random
programming
tutorial

Get a random boolean in python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Generating a random Boolean in Python is simple, but the best method depends on whether you want a fifty-fifty result, a weighted probability, or reproducible test behavior. The random module already gives you all three patterns, so the main job is choosing the one that matches your intent.

The Simplest 50-50 Boolean

For an even split between True and False, random.choice is the most readable option.

python
1import random
2
3value = random.choice([True, False])
4print(value)

This is explicit and easy for other developers to understand. If clarity matters more than micro-performance, it is a strong default.

A Bit-Level Alternative

If you want a direct random bit, getrandbits(1) returns either 0 or 1. Wrapping it with bool converts the result to a Boolean.

python
1import random
2
3value = bool(random.getrandbits(1))
4print(value)

This is compact and efficient. It is especially natural if the surrounding code already works with random bits or integer flags.

Weighted Random Booleans

A fifty-fifty split is not always what you want. If True should happen with probability p, compare a random float against that threshold.

python
1import random
2
3p = 0.8
4value = random.random() < p
5print(value)

Here True occurs about eighty percent of the time. This pattern is the right choice for simulations, feature rollout experiments, and randomized behavior with a known bias.

Reproducibility for Tests

Random behavior is often undesirable in tests unless it is reproducible. Python lets you fix the random sequence with a seed.

python
1import random
2
3random.seed(1234)
4values = [bool(random.getrandbits(1)) for _ in range(5)]
5print(values)

A fixed seed does not make the values less random in structure; it makes the sequence deterministic so the same test can be repeated reliably.

Use secrets for Security-Sensitive Choices

The random module is fine for simulations, test data, and game logic, but it is not designed for security decisions. If a Boolean affects authentication, token flow, or anything attacker-visible, use the secrets module instead.

python
1import secrets
2
3value = bool(secrets.randbits(1))
4print(value)

That keeps the interface simple while switching to a cryptographically stronger source of randomness.

Which Method Should You Prefer

A practical rule is:

  • use random.choice([True, False]) for readability
  • use bool(random.getrandbits(1)) for a compact unbiased Boolean
  • use random.random() < p for weighted probabilities

All three are correct. The wrong choice is usually not technical correctness but unclear intent. If the reader has to infer whether the probability is meant to be fifty-fifty or biased, the code is doing a poor job of expressing the requirement.

If you need many random Booleans, the same rule still applies. Generate them in a loop or comprehension with the method that matches the probability model, rather than trying to force one helper into every use case.

Common Pitfalls

  • Using random.randint(0, 1) and forgetting to convert the integer to a Boolean.
  • Hard-coding 0.5 comparisons when the real requirement is a configurable probability.
  • Forgetting to set a seed in tests that depend on deterministic behavior.
  • Treating readability as unimportant when random.choice is clearer for simple cases.
  • Assuming all random Boolean generation should use the same method regardless of whether the result is weighted.

Summary

  • 'random.choice([True, False]) is the clearest simple answer.'
  • 'bool(random.getrandbits(1)) gives a compact random bit as a Boolean.'
  • 'random.random() < p is the right pattern for weighted truth values.'
  • Use random.seed(...) when you need reproducible sequences in tests.
  • Choose the method that makes the intended probability obvious.

Course illustration
Course illustration

All Rights Reserved.