Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท Random Numbers

Pseudorandom Numbers and Seeds

Learn how computers make 'random' numbers, why seeds make runs repeatable, and how to use seeds safely in Monte Carlo experiments.

Every Monte Carlo method starts with random numbers. But a computer is a deterministic machine: given the same instructions, it does the same thing. So where does randomness come from? In practice, simulations use pseudorandom numbers: sequences produced by a fixed formula that look random enough for the job. This lesson explains how they work, what a seed is, and why seeds are your friend.

True random versus pseudorandom

  • True random numbers come from physical processes: electronic noise, radioactive decay, timing of keystrokes. They are unpredictable but slow and cannot be replayed.
  • Pseudorandom numbers come from an algorithm called a pseudorandom number generator (PRNG). It keeps an internal state (a number or block of numbers). Each request updates the state and returns a value derived from it.

For simulation, pseudorandom numbers are usually better. They are fast, and you can replay an experiment exactly, which is essential for debugging and for sharing results.

flowchart LR
  S["seed"] --> A["initial state"]
  A --> B["update state"]
  B --> C["output number"]
  B --> B

A PRNG: the seed sets the starting state; every output moves the state forward.

What a seed does

The seed is the number that sets the initial state. Same seed, same algorithm, same sequence, always. A different seed gives a different (but equally random-looking) sequence.

Python Same seed, same numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first two lines print the same list; the third differs. This is reproducibility: anyone with your code and seed can reproduce your numbers.

Uniform random numbers

The workhorse output of a PRNG is a uniform random number U on the interval [0, 1): every value in that range is equally likely. Almost all other distributions (normal, exponential, discrete) are built from uniform numbers, as Chapter 3 shows. A uniform number has mean 0.5 and standard deviation about 0.2887 (that is 1/โˆš12).

Python Check the mean and spread of uniform numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Seeds in practice: good habits

  1. Set a seed while developing. Bugs that depend on random values become reproducible.
  2. Record the seed with every result you report.
  3. Do not hunt for a "good" seed. Trying seeds until the answer looks right is cheating; it throws away the very uncertainty you wanted to measure.
  4. For final production runs, test several seeds. Results should agree within their error bars. If they do not, you have a bug or too few samples.
  5. Do not use the same seed for runs that should be independent. Two experiments seeded identically use identical random numbers, so their errors are linked.
Python Estimates from different seeds

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here the darts fall in the unit square [0, 1]ยฒ, and the quarter circle xยฒ + yยฒ โ‰ค 1 has area ฯ€/4, so the factor 4 still applies. The spread across seeds is an honest picture of the Monte Carlo error.

Seeding from the clock

If you do not set a seed, Python and NumPy pick one from the operating system, such as the current time or hardware entropy, so each run differs. That is right for an exploratory run, but remember you cannot replay it unless you saved the seed.

Seeded PRNGs from general libraries are fine for simulation but not secure. Never use them for passwords, tokens or cryptography. Use a cryptographic source such as Python's secrets module for those.

Period and state size

A PRNG's state is finite, so eventually the sequence repeats. The length before it repeats is the period. Python's random module and NumPy's default generator have enormous periods (astronomically larger than any simulation will use), so repetition is never a practical issue for them. Simple generators, like the toy one in the next lesson, can have tiny periods, which is why you should not write your own for real work.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a seed?

    The seed initializes the generator's state; the same seed always produces the same sequence.
  2. 2. Why is it bad practice to try many seeds and keep the one that gives the 'best' result?

    Choosing a seed to get a nice answer is selection bias: it throws away the randomness error you were trying to measure.
  3. 3. A uniform random number on [0, 1) has mean

    By symmetry the average of a uniform number on [0, 1) is 0.5.
  4. 4. Which use is NOT appropriate for a simulation PRNG such as NumPy's default generator?

    Simulation generators are predictable if someone learns the state; security tokens need a cryptographic source.

Summary

  • Pseudorandom numbers come from a deterministic algorithm with an internal state.
  • The seed sets the initial state: same seed means the same sequence, which makes experiments reproducible.
  • Uniform numbers on [0, 1) are the raw material for all other random variables.
  • Record your seeds, never cherry-pick them, and compare several seeds for final results.
  • Simulation generators are not suitable for security.

Next: Linear Congruential Generators