Python Weighted Random
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Weighted random selection means some outcomes should be more likely than others. In Python, the standard answer is usually random.choices, but the full solution also depends on whether you need sampling with replacement, reproducible results, or cryptographic randomness.
The Standard Tool: random.choices
For most application code, random.choices is the simplest way to draw weighted outcomes.
This chooses ten results, where bronze is most likely and gold is least likely.
The weights are relative. They do not need to add up to 100 or to 1.0.
Understand Sampling With Replacement
random.choices samples with replacement. That means the same item can appear multiple times.
Repeated values are normal here. If the requirement is "draw multiple unique items with weights," that is a different problem.
Validate Inputs
Weighted selection only makes sense if the inputs are valid. A small validation helper prevents hard-to-debug bias or runtime errors.
Negative weights and all-zero weights should be treated as invalid input, not as special cases to ignore.
Reproducibility With a Dedicated Random Generator
If you are writing tests, simulations, or data pipelines, avoid depending on the process-wide global random state. Create a Random instance with a seed.
Using a dedicated generator makes runs easier to reproduce and isolates your code from other random operations happening elsewhere in the process.
Repeated Sampling Can Use Cumulative Weights
Python also supports cumulative weights, which are useful if you already have them precomputed.
This can be convenient when the cumulative distribution is already available from upstream code.
Without Replacement Is Different
Python's built-in random.sample does not support weights. If you need weighted sampling without replacement, you need a custom algorithm or a third-party library such as NumPy, depending on the scale and constraints of the task.
For many basic applications, though, weighted sampling with replacement is exactly what is intended.
Security-Sensitive Selection
Do not use the random module for security-sensitive behavior such as token selection or secret generation. Python's random module is designed for simulation and general-purpose randomness, not for cryptographic security.
If security matters, use the secrets module, though it does not provide weighted selection directly.
Common Pitfalls
The biggest pitfall is forgetting that random.choices samples with replacement. If duplicates in the result seem surprising, the underlying requirement was probably weighted sampling without replacement.
Another issue is assuming weights must be normalized first. They do not. Relative magnitudes are what matter.
Developers also forget to validate inputs, especially when weights come from configuration or external data. A negative or mismatched weight list should fail fast.
Finally, do not use random for security-sensitive logic. Reproducible pseudo-randomness is useful, but it is not cryptographic randomness.
Summary
- Use
random.choicesfor standard weighted random selection in Python. - Weights are relative and do not need to sum to a fixed total.
- '
random.choicessamples with replacement, so duplicates are expected.' - Use a seeded
Randominstance when reproducibility matters. - Use a different approach if you need weighted sampling without replacement or cryptographic security.
Related reading
- Python what are the advantages of async over threads?
- Python, what''s the Enum type good for?
- Python Why is functools.partial necessary?
- Python xml ElementTree from a string source?
- Python ZeroMQ connecting two different clients together in a ROUTER and a REP configuration
- Python ZeroMQ PUSH/PULL logic, set high water mark to a low end puller without losing any message
- Pythonic way to avoid if x return x statements
- Pythonic way to check if a file exists?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.