Why & How to Set a Random Seed in R (Example) | set.seed Function Explained

 

In this R programming tutorial you’ll learn how to specify a random seed using the set.seed() function.

Table of contents:

Let’s start right away…

 

Reasons for Using a Random Seed in R

A random seed (or seed state) is a number that initializes a pseudorandom number generator.

Pseudorandom numbers are not truly random, but they aim to be as patternless as possible to simulate randomness.

Why is this important when using the R programming language?

In R, we can set a random seed to make the output of our R code reproducible. By setting a specific seed, the random processes in our script always start at the same point and hence lead to the same result.

Let’s do this in practice…

 

Example: Setting Random Seed Using set.seed() Function in R

In this example, I’ll show what happens if you don’t use a random seed and how you can use the set.seed function to set a seed in R.

First, let’s generate some random numbers in R using the rpois function:

rpois(5, 3)            # Generate random numbers without seed
# [1] 1 3 3 2 6

The output of the previous R syntax is a numeric vector with the elements 1, 3, 3, 2, and 6.

Let’s execute exactly the same R code again:

rpois(5, 3)            # Generate random numbers again
#[1] 3 6 3 1 2

Now, the result is a numeric vector consisting of the vector elements 3, 6, 3, 1, and 2.

As you can see, the output is completely different even though we have used exactly the same R code.

This can make it impossible to replicate a specific output and that makes it difficult to retrace the steps the programmer of the R code has made.

For that reason, it is considered as best practice for researchers to set a random seed at the beginning of R scripts that involve random processes.

Let’s see how we can do that in R!

The basic installation of the R programming language provides the set.seed function. Within the set.seed function, we simply have to specify a numeric value to set a seed.

Have a look at the following R code:

set.seed(12345)        # Set seed for reproducibility
rpois(5, 3)            # Generate random numbers with seed
# [1] 4 5 4 5 3

We generated the random sequence 4, 5, 4, 5, and 3.

Let’s do this again with the same seed as before:

set.seed(12345)        # Set same random seed
rpois(5, 3)            # Generate random numbers with same seed
# [1] 4 5 4 5 3

The output is exactly the same – great!

 

Video & Further Resources

In case you need more information on the topics of this tutorial, you might want to watch the following video of my YouTube channel. I’m illustrating the R programming code of this tutorial in the video:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might read the related posts on this homepage:

 

Summary: At this point you should have learned how to apply the set.seed function in the R programming language. Let me know in the comments section below, if you have additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top