← Back to R Course | Chapter 9: Statistics | Lesson 6 of 7

set.seed()

In this page:

  1. set.seed()

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()

markup
set.seed(123)
print(sample(1:100, 5))

set.seed(123)
print(sample(1:100, 5))  # identical output, same seed
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.