Select 50 items from list at random
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Selecting 50 random items sounds easy, but correctness depends on whether duplicates are allowed and whether selection must be reproducible. The right method changes based on those requirements. Most use cases need sampling without replacement, which is best handled by standard library sampling utilities.
Sampling Without Replacement
If each chosen item must be unique, use random.sample.
This guarantees 50 unique picks, assuming source list has at least 50 elements.
Reproducible Results with Seeds
For tests and experiments, deterministic sampling is useful.
Using a local Random instance avoids global random state side effects.
Sampling With Replacement
If duplicates are allowed, use random.choices.
This is different from sample and should be chosen intentionally.
Handling Small Lists Safely
random.sample raises ValueError when sample size exceeds input size. Add explicit checks for robust behavior.
This makes failure mode clear for callers.
Large Dataset Considerations
For very large iterables that do not fit in memory, consider reservoir sampling. It selects k random elements from a stream in one pass.
Use this when data arrives incrementally.
Weighted Random Selection
If some items should be more likely, use weighted sampling with replacement.
For weighted sampling without replacement, use dedicated libraries or custom algorithms.
Cryptographic vs Statistical Randomness
random module is designed for simulation and general-purpose sampling, not security-sensitive choices. If item selection controls tokens, vouchers, or anything adversarial, use secrets and redesign for uniqueness guarantees.
For exactly 50 unique secure picks, shuffle with a secure random strategy or use a vetted crypto library that supports secure sampling without replacement.
Avoid Bias from Manual Index Math
Developers sometimes write int(random.random() * n) and accidentally introduce subtle bias when converting floating-point values. Library sampling methods avoid these pitfalls and handle edge cases better.
Use high-level APIs first, then optimize only when profiling proves it necessary.
Testing Random Selection Logic
For random code, test invariants rather than exact values unless seeded:
- result size equals expected
k - uniqueness constraints hold when required
- selected items are members of source set
With fixed seed, snapshot tests can verify deterministic behavior.
Common Pitfalls
- Using
choiceswhen unique selection is required. - Forgetting to check list length before
sample. - Relying on global random seed in large applications.
- Testing exact random output without deterministic seed.
- Ignoring stream-size constraints in large data workflows.
Summary
- Use
random.samplefor unique random selection. - Use
random.choiceswhen duplicates are acceptable. - Add explicit checks for sample-size constraints.
- Use local seeded RNG for reproducible behavior.
- For large streams, use reservoir sampling to keep memory bounded.
Related reading
- Select an element from a stream with uniform distributed probability
- Select combination of elements from array whose sum is smallest possible positive number
- Select k random elements from a list whose elements have weights
- Select N random elements from a List efficiently without toArray and change the list
- Select a DictionaryT1, T2 with LINQ
- SELECT Specific Value from map
- Select n records at random from a set of N
- Select top N elements of related objects

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.