Unit 01.02: Seed once, at the top
A bootstrap interval you cannot reproduce is a number you cannot defend.
Seed once, at the top
Simulation, bootstrapping, permutation tests, cross-validation and random assignment all draw random numbers. Without a seed, every run gives different answers — usually similar, occasionally different enough to change what you report.
set.seed() makes the sequence deterministic. Put it once at the top of the script, not beside each draw: scattering seeds through a file makes the results depend on execution order, which is worse than not seeding at all because it looks reproducible.
The seed value itself is arbitrary and should be. Choosing a seed because it gave a nicer result is a form of fishing.
This block draws the same sample twice with the seed reset, and once without.
# Anything random must be seeded, or your confidence interval is unrepeatable.
set.seed(2026)
first <- mean(rnorm(50, mean = 100, sd = 15))
set.seed(2026)
second <- mean(rnorm(50, mean = 100, sd = 15))
third <- mean(rnorm(50, mean = 100, sd = 15)) # no reseed
cat("Seeded run 1:", round(first, 4), "\n")
cat("Seeded run 2:", round(second, 4), " identical:", first == second, "\n")
cat("Unseeded next:", round(third, 4), " identical:", first == third, "\n\n")
# The seed belongs at the top of the script, once, not beside each draw.
cat("Difference between the two unseeded-position results:",
round(abs(first - third), 4), "\n")
cat("On this sample that is", round(abs(first - third) / first * 100, 2),
"% -- small, and quite enough to change a reported interval's endpoints.\n")
The two seeded runs both give 99.756 and compare as identical. The third draw, taken without reseeding, gives 97.3027 — a difference of 2.4533, about 2.46% of the estimate. That is small, and it is quite large enough to move the endpoints of a reported interval, which is the number a reader will quote.
The mistake this prevents
The mistake is seeding inside a loop or a function, which makes every iteration produce the identical 'random' draw. The simulation then has no variation in it at all, and the standard error it reports is zero or nearly so.
Takeaway
Call set.seed() once at the top of any script that uses randomness, record the value in the report, and never choose the seed after seeing what it does to the answer.
