Set Seed Function

Hi,

I'd like some help understanding the set.seed function. I'm new to coding and don't understand whats set.seed(123) does.

From what I read, it randomizes the data frame but (123) sounds like it puts it in order.

"Please explain it to me like I'm a 5 year old" lol.

Cheers!

The set.seed() function is used to set a Random seed which Pseudorandom number generators use when generating "random" numbers. The thing is that the values generated by pseudorandom number generators aren't truly random, but rather determined by an initial value called the seed.

set.seed() can be used to ensure reproducibility when running simulations. For example using R's random number generator rnorm(), we can generate the same sequence of random numbers multiple times:

> set.seed(5)
> rnorm(5)
[1] -0.84085548  1.38435934 -1.25549186  0.07014277  1.71144087
> set.seed(5)
> rnorm(5)
[1] -0.84085548  1.38435934 -1.25549186  0.07014277  1.71144087

Read more:

1 Like

This sheds some light @ADernild. Thank you.

But I saw set.seed(123) in an example, so essentially it could've been any number instead of 123 used?

Is 123 just a number to recall the same set of random numbers used?

Could the example have used 321 instead for the same purpose?

If I was writing the code in a script, would I want to use a specific number or any number is fine just as long as it is noted for use if I'd like to use same sequence again?

Yes, that's correct. You can use any seed you want as long as it is an integer. We use seeds so we get the same "random" numbers each time. In homework assignments, a professor might ask to use a seed so the homework is all the same but that's the only time it would really matter to use the seed in an example or template script. Otherwise, when you begin a script, you can specify whatever seed you want. 123 isn't special.

Seeds are useful in production in my work when I'm drawing a sample that we will then mail survey invitations to. If someone re-runs the script, we get the same sample. We set it at the beginning and never change it because we don't want to get a different sample.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.