Skip to content

Seeds and simulation

Random numbers on a computer are repeatable by design. Learn what a seed does, why Python and R usually disagree, and how CodeSplash gets them to match.

Applied ~11 min8 steps

What runs in CodeSplash

You will be able to

  • Explain what a seed does and what it doesn't
  • Estimate the Monte Carlo error of a simulated result
  • Use a shared generator to get matching draws in Python and R

Read first

01

Random, on purpose

Read

Computers produce random-looking numbers with a formula, starting from a seedRandom seedThe starting point of a random-number stream; the same seed reproduces the same resamples.. The same seed gives the same sequence.

That matters a great deal. A simulationMonte Carlo simulationEstimating a quantity by drawing many random samples and summarizing the results. you can't repeat is a result nobody can check, including you next month.

02

Same seed, two languages

Predict

Predict first

You call np.random.default_rng(1) in Python and set.seed(1) in R, then draw one number from each. What do you get?

The same drawsDifferent drawsDifferent on some computers
Reveal

Different draws. Each language has its own generator and its own way of using a seed. So two correct scripts can give slightly different simulated answers.

03

How CodeSplash makes them match

Explore

CodeSplash's helpers share one generator, Wichmann–Hill, written identically in both languages. Run both blocks in a notebook: the numbers match to every digit.

Python
from codesplash.stats.rng import Rng

print(Rng(42).uniform(3))
print(Rng(42).normal(2))
R
library(codesplashr)

print(cs_runif(cs_rng(42), 3), digits = 17)
print(cs_rnorm(cs_rng(42), 2), digits = 17)

That's why a permutation test on tooth growth gives the same count, 130, in both engines.

04

How much is noise?

Read

A simulated result has its own error. For a simulated p-value from BB shuffles:

Monte Carlo SE≈p(1−p)B\text{Monte Carlo SE} \approx \sqrt{\frac{p(1 - p)}{B}}

With 1,999 shuffles and p = .066, that's about 0.0060.006. So a change of 0.0010.001 means nothing, while a value near your threshold may call for more shuffles.

05

Change the seed

Explore

Change the Shuffles seed a few times. How much does Permutation p move? That movement is Monte Carlo noise, and it's separate from sampling variability.

BExplorable · Permutation shuffler

Could a difference this large come from shuffling the labels alone?

Shuffle the labels a thousand times. How often will the shuffled difference be as large as the real one?

How sure?

Data

Statistic

+30 on the last value of the second group.

Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

06

Check your reading

Checkpoint

Check yourself

You set a seed and get p = 0.048. What does the seed guarantee?

Show the answer

Answer: a

A seed gives reproducibility of the computation. It says nothing about accuracy, or about whether a new study would agree.

Check yourself

A simulated p-value is about 0.50.5, from B=2,500B = 2{,}500 shuffles. What is its Monte Carlo standard error? Give two decimals.

Show the answer

Answer: 0.01

0.5×0.5/2500=0.5/50=0.01\sqrt{0.5 \times 0.5 / 2500} = 0.5 / 50 = 0.01.

Check yourself

A colleague tries seeds until a bootstrap interval excludes zero, and reports that one. What's the problem?

Show the answer

Answer: b

Pick the seed once, before you see results, and record it. If a result changes when the seed does, it wasn't stable to begin with.

07

Set it in the app

In the app

Methods that resample have a Reproducibility section with a Seed field in Analyze, and the seed goes into the generated code. Set a seed, run in Python, rerun in R, and compare.

08

Recap

Recap
  • A seed makes a simulation repeatable, and it doesn't make it right.
  • Python and R have different generators, unless you use a shared one.
  • Simulated results carry Monte Carlo error. Estimate it, and raise the draws if it matters.
  • Fix the seed once, and never search for a good one.

Concepts in this lesson