set.seed()
In this page:
set.seed()
set.seed(n) initializes R's random number generator to a fixed, known starting state, making subsequent "random" operations like sample() or rnorm() reproducible across runs. Without it, each run produces different random results. Setting a seed is essential for sharing reproducible analysis and for testing code that involves randomness.
Note: Always call set.seed() before any random operation in code you want others to be able to reproduce exactly.
Example: set.seed()
set.seed(123)
print(sample(1:100, 5))
set.seed(123)
print(sample(1:100, 5)) # identical output, same seed
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: