Open In App

Generate Data sets of same Random Values in R Programming – set.seed() Function

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The set.seed() function in R ensures that random number generation is consistent across different sessions, allowing for identical results each time the code is executed. This is particularly important when sharing code with others or when results need to be verified.

Syntax:

set.seed(n)

Where:

  • n: seeds for repeatable data sets

Why set.seed() is required

In R, the set.seed() function is not mandatory for all analyses. However, it is recommended to use it in some instances. We know that functions like rnorm(), runif(), and sample() produce different results each time they are called, setting a seed ensures that the sequence of random numbers can be reproduced. This allows for:

  • Reproducibility: Ensuring that analyses can be repeated with the same data and results.
  • Debugging: Identifying and fixing issues in code by providing consistent outputs.
  • Collaboration: Allowing others to verify results by running the same code with the same data.

Example: Creating Reproducible Random Data Sets

In this example we will use the set.seed() to generate reproducible random data. By setting the seed to 123, the sequence of random numbers generated by rnorm(10) will be the same each time this code is executed.

R
set.seed(123)

random_data <- rnorm(10)

print(random_data)

Output:

[1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499
[7] 0.46091621 -1.26506123 -0.68685285 -0.44566197

Verifying Reproducibility

To confirm that the random number generation is reproducible, we can compare two data sets generated with the same seed using the identical() function.

R
set.seed(123)
dt_1 <- rnorm(10)

set.seed(123)
dt_2 <- rnorm(10)

identical(dt_1, dt_2)

Output:

TRUE

Since both data sets are identical, this confirms that setting the seed ensures reproducibility.

In this article, we will discuss how we Generate Data sets of the same Random Values in the R Programming Language using set.seed() Function.



Next Article
Article Tags :

Similar Reads