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.
What runs in CodeSplash
- Permutation test (two independent groups) Verified in Python and R
- Bootstrap confidence interval Verified in Python and R
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
Random, on purpose
ReadComputers 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.
Same seed, two languages
PredictPredict 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?
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.
How much is noise?
ReadA simulated result has its own error. For a simulated p-value from shuffles:
With 1,999 shuffles and p = .066, that's about . So a change of means nothing, while a value near your threshold may call for more shuffles.
Change the seed
ExploreChange 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?
Data
Statistic
+30 on the last value of the second group.
Commit your prediction above to see the result
Make your prediction to see the result.
Check your reading
CheckpointCheck 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 , from shuffles. What is its Monte Carlo standard error? Give two decimals.
Show the answer
Answer: 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.
Set it in the app
In the appMethods 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.
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