Introduction

Author

Clayton Cafiero

Published

2025-01-05

Here we will learn about some of the abundant uses of randomness. Randomness is useful in games (shuffle a deck, roll a die), but it’s also useful for modeling and simulating a staggering variety of real world processes.

Learning objectives

  • You will understand why it is useful to be able to generate pseudo-random numbers or make pseudo-random choices in a computer program.
  • You will learn how to use some of the most commonly-used methods from Python’s random module, including
    • random.random() to generate a pseudo-random a floating point number in the interval [0.0, 1.0),
    • random.randint() to generate a pseudo-random integer in a specified interval,
    • random.choice() to make a pseudo-random selection of an item from an iterable object,
    • random.shuffle() to shuffle a list,
    • random.sample() to sample k elements from a population without replacement, and
    • random.gauss() to sample from a normal (Gaussian) distribution given mean and standard deviation.
  • You will understand the role of a seed in the generation of pseudo-random numbers, and understand how setting a seed makes predictable the behavior of a program incorporating pseudo-randomness.

Terms introduced

  • bell curve
  • deterministic
  • gambler’s ruin
  • Mersenne twister
  • Monte Carlo simulation
  • normal (Gaussian) distribution
  • pseudo-random
  • random module
  • random walk
  • sample
  • seed
  • shuffle

For mean and standard deviation, see Chapter 14: Data Analysis and Presentation.

Copyright © 2023–2025 Clayton Cafiero

No generative AI was used in producing this material. This was written the old-fashioned way.