![]() |
VOOZH | about |
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.
set.seed(n)
Where:
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:
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.
Output:
[1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499
[7] 0.46091621 -1.26506123 -0.68685285 -0.44566197
To confirm that the random number generation is reproducible, we can compare two data sets generated with the same seed using the identical() function.
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.