Generate sample with set.seed function in R

What is set.seed function in R and why to use it ? : set.seed function in R is used to reproduce results i.e.  it produces the same sample again and again. When we generate randoms numbers without set.seed() function it will produce different samples at different time of execution. let see how to generate stable sample of random numbers with set.seed() function in R with example

Syntax for set.seed function in R:

set.seed(n)

where n is a seed number which is an integer value.

The seed number (n) you choose is the starting point used in the generation of a sequence of random numbers. Which is why you’ll obtain the same results given the same seed number.

Example of set.seed function in R:

rnorm(5)

rnorm(5)

when we run above rnorm function, it will generate different set of five numbers at each time

output:

[1]  1.7150650  0.4609162 -1.2650612 -0.6868529 -0.4456620

[1]  1.2240818  0.3598138  0.4007715  0.1106827 -0.5558411

 

 

 

# set.seed function in R

set.seed(123)
rnorm(5)

set.seed(123)
rnorm(5)

when we run above rnorm function, it will generate same set of five numbers at each time because of set.seed() function

output:

[1] -0.56047565 -0.23017749  1.55870831  0.07050839  0.12928774 

[1] -0.56047565 -0.23017749  1.55870831  0.07050839  0.12928774

 

 

Example 2 of set.seed function in R:

sample(LETTERS, 6)

sample(LETTERS, 6)

when we run above sample function, it will generate different set of 6 letters at each time

output:

[1] “Y” “L” “Q” “N” “C” “S”

[1] “G” “B” “H” “V” “T” “O”

 

 

 

# set.seed function in R

set.seed(321)
sample(LETTERS, 6)

set.seed(321)
sample(LETTERS, 6)

when we run above sample function, it will generate same set of 6 letters at each time because of set.seed() function

output:

[1] “Y” “X” “F” “Z” “I” “H” 

[1] “Y” “X” “F” “Z” “I” “H”

previous small set.seed function in r                                                                                                                next small set.seed function in r