AI Skill Report Card

Using Random Module

B68·Sep 5, 2026·Source: Web
14 / 15
Python
import 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
11 / 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 secrets instead if cryptographic security is needed
  • Validate ranges/inputs (e.g., k <= len(population) for sample)

Function Reference

GoalFunctionNotes
Float in [0.0, 1.0)random.random()Base generator
Float in [a, b]random.uniform(a, b)
Int in [a, b] inclusiverandom.randint(a, b)
Int in range with steprandom.randrange(start, stop, step)
Single random elementrandom.choice(seq)Raises IndexError if empty
Multiple elements w/ replacementrandom.choices(population, weights=None, k=1)Supports weights
Unique elements w/o replacementrandom.sample(population, k)k must be ≤ population size
Shuffle in placerandom.shuffle(seq)Mutates list; no return value
Gaussian/normal distributionrandom.gauss(mu, sigma) or random.normalvariate(mu, sigma)
Other distributionsrandom.expovariate, random.betavariate, random.gammavariate, random.lognormvariate, random.triangular, random.vonmisesvariate, random.weibullvariate
Reproducibilityrandom.seed(a=None)Same seed → same sequence
Save/restore staterandom.getstate() / random.setstate(state)
Independent generator instancerandom.Random(seed)Use when isolating streams (e.g., multithreading)
Cryptographically secure randomUse secrets module insteadrandom 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
12 / 20

Example 1: Reproducible shuffle for testing Input:

Python
import 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:

Python
import 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:

Python
import 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
  • Always seed (random.seed(n)) in tests and simulations that need reproducible output.
  • Use random.sample() for unique picks; use random.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.
  • Do NOT use random for cryptography — it is a Mersenne Twister PRNG, predictable and unsuitable for security purposes. Use secrets instead.
  • Do NOT call random.sample() with k > len(population) — raises ValueError.
  • Do NOT expect random.shuffle() to return a value — it shuffles in place and returns None.
  • 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.
0
Grade BAI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
14/15
Workflow
11/15
Examples
12/20
Completeness
14/20
Format
13/15
Conciseness
14/15