R / CHAPTER 13

Reproducible sampling and simulation

Advanced

Control random state while keeping statistical claims honest.

By the end of this chapter

  • Repeat a simulation with a controlled seed
  • Separate replacement policy from sample size
  • Test properties without relying on one lucky sample

The idea

Randomized analysis needs both a statistical design and a reproducibility record. Set a seed before a repeatable run, and record the relevant R version and random-generator settings when exact cross-environment reproduction matters. A seed determines a sequence under those settings; it does not guarantee that a small sample is representative or that an estimator is unbiased.

Sampling with replacement allows the same observation to appear several times, while sampling without replacement does not. Write that choice into the experiment rather than accepting a default accidentally. Do not reset the same seed inside every replicate when independent simulated draws are intended. Test invariant properties, such as output size and permitted values, alongside a controlled reproducibility check.

R example
set.seed(42)
a <- sample.int(10,4)
set.seed(42)
b <- sample.int(10,4)
cat(identical(a,b),"\n",sep="")
Result
TRUE

Both runs start from the same random state with the same settings and operations, so their sampled sequences match.

Try it yourself

Simulate repeated sums of sampled values and record the seed and replacement policy.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

A simulation resets the same seed before every otherwise identical replicate. What happens?