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.
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.
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.
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.
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.
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() < pfor 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.5comparisons 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.choiceis 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() < pis 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.

