AI Skill Report Card
Using Random Module
Quick Start14 / 15
Pythonimport random # Random float in [0.0, 1.0) random.random() # Random integer in inclusive range random.randint(1, 10) # Random choice from a sequence random.choice(['rock', 'paper', 'scissors']) # Shuffle a list in place deck = list(range(52)) random.shuffle(deck) # Sample without replacement random.sample(range(100), k=5) # Reproducible results random.seed(42)
Recommendation▾
Verify actual output values for the seeded examples—Python's random outputs must be exact and reproducible, or replace with a caveat noting they're illustrative rather than verified
Workflow11 / 15
Progress:
- Identify the randomization need (number, choice, sequence, distribution)
- Pick the correct function (see table below)
- Seed the generator if reproducibility is required (tests, simulations)
- Use
secretsinstead if cryptographic security is needed - Validate ranges/inputs (e.g.,
k <= len(population)forsample)
Function Reference
| Goal | Function | Notes |
|---|---|---|
| Float in [0.0, 1.0) | random.random() | Base generator |
| Float in [a, b] | random.uniform(a, b) | |
| Int in [a, b] inclusive | random.randint(a, b) | |
| Int in range with step | random.randrange(start, stop, step) | |
| Single random element | random.choice(seq) | Raises IndexError if empty |
| Multiple elements w/ replacement | random.choices(population, weights=None, k=1) | Supports weights |
| Unique elements w/o replacement | random.sample(population, k) | k must be ≤ population size |
| Shuffle in place | random.shuffle(seq) | Mutates list; no return value |
| Gaussian/normal distribution | random.gauss(mu, sigma) or random.normalvariate(mu, sigma) | |
| Other distributions | random.expovariate, random.betavariate, random.gammavariate, random.lognormvariate, random.triangular, random.vonmisesvariate, random.weibullvariate | |
| Reproducibility | random.seed(a=None) | Same seed → same sequence |
| Save/restore state | random.getstate() / random.setstate(state) | |
| Independent generator instance | random.Random(seed) | Use when isolating streams (e.g., multithreading) |
| Cryptographically secure random | Use secrets module instead | random is NOT secure for security tokens/passwords |
Recommendation▾
This skill documents a standard library module Claude already knows well; the value is mainly in the pitfalls/best-practices sections, so consider trimming the function reference table since it's largely redundant with Claude's built-in knowledge
Examples12 / 20
Example 1: Reproducible shuffle for testing Input:
Pythonimport random random.seed(0) items = ['a', 'b', 'c', 'd'] random.shuffle(items) print(items)
Output: ['b', 'a', 'd', 'c'] (deterministic given seed 0; exact output depends on Python version)
Example 2: Weighted random choice Input:
Pythonimport random random.seed(1) random.choices(['red', 'green', 'blue'], weights=[10, 1, 1], k=5)
Output: ['red', 'red', 'blue', 'red', 'green'] (red heavily favored due to weight)
Example 3: Sampling without replacement Input:
Pythonimport random random.seed(2) random.sample(range(1, 50), k=6) # lottery-style draw
Output: [27, 15, 45, 8, 33, 3] (6 unique numbers, no duplicates)
Recommendation▾
Add a slightly more complex real-world example, such as a Monte Carlo simulation or weighted random sampling for a game/test scenario, to justify the skill's existence beyond documentation lookup
Best Practices
- Always seed (
random.seed(n)) in tests and simulations that need reproducible output. - Use
random.sample()for unique picks; userandom.choices()when duplicates/weights are needed. - Use
random.Random()instances to create isolated generators for concurrent/multithreaded code instead of the global default instance. - Prefer
secrets.token_hex(),secrets.choice(), etc. for tokens, passwords, or anything security-sensitive. - For large populations,
random.sample()is more memory-efficient than shuffling the entire population.
Common Pitfalls
- Do NOT use
randomfor cryptography — it is a Mersenne Twister PRNG, predictable and unsuitable for security purposes. Usesecretsinstead. - Do NOT call
random.sample()withk > len(population)— raisesValueError. - Do NOT expect
random.shuffle()to return a value — it shuffles in place and returnsNone. - Do NOT forget that unseeded runs differ every execution — seed explicitly when deterministic output matters (tests, reproducible research).
- Avoid reusing the same seed across unrelated randomization calls — this can introduce unintended correlations between "independent" random draws.