The random module

18-minute lesson · module-modules-03

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

The random module

18-minute lesson · module-modules-03

  • Generate random integers with `random.randint` and shuffle with `random.shuffle`.
  • Pick a random element with `random.choice`.
  • Explain why pseudo-random numbers are reproducible.
  • Seed the generator with `random.seed` so results are deterministic and testable.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Modules / The random module

Adaptation notes. Reading rewritten in our voice. The dice / card-deck examples are ours. Because hidden tests must be deterministic, the exercise functions take a seed argument and call random.seed(seed) first; the test recomputes the expected values with the same seed and asserts an exact match.

License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.