What is the use of set.seed in R?


The set.seed helps to create the replicate of the random generation. If the name of the object changes that does not mean the replication will be changed but if we change the position then it will. Here, in the below example x4 in the first random generation and the x_4 in the second random generation with the same set.seed are same but x4 and x4 in both are different.

Example

 Live Demo

set.seed(101)
x1<−rnorm(50)
x1

Output

[1] −0.3260365 0.5524619 −0.6749438 0.2143595 0.3107692 1.1739663
[7] 0.6187899 −0.1127343 0.9170283 −0.2232594 0.5264481 −0.7948444
[13] 1.4277555 −1.4668197 −0.2366834 −0.1933380 −0.8497547 0.0584655
[19] −0.8176704 −2.0503078 −0.1637557 0.7085221 −0.2679805 −1.4639218
[25] 0.7444358 −1.4103902 0.4670676 −0.1193201 0.4672390 0.4981356
[31] 0.8949372 0.2791520 1.0078658 −2.0731065 1.1898534 −0.7243742
[37] 0.1679838 0.9203352 −1.6716048 0.4484691 0.4824588 0.7582138
[43] −2.3193274 −0.4595048 −1.1053837 0.4029283 0.5689349 −0.7060833
[49] −0.2900906 −1.4838781

Example

 Live Demo

x2<−rnorm(50)
x2

Output

[1] −1.150255281 −0.274471162 0.577901003 −1.396902647 0.749057716
[6] −1.051186697 0.165380871 1.129809120 1.173722464 −0.427863232
[11] −0.259802108 −1.411173044 −0.641357554 0.112457509 0.422604331
[16] 0.386835291 −0.687798326 0.148902489 −0.057649748 −0.074823365
[21] 1.509897438 1.619937008 1.153158167 −0.077603595 −1.818934501
[26] −1.037444583 0.302492246 −1.277946167 0.138339048 −0.050984124
[31] 1.852147575 1.111675270 −0.511375322 −0.543881104 −1.728927284
[36] 0.470749539 0.005387122 1.348045786 0.724096713 1.552549165
[41] 1.325469832 −0.034265092 −0.361013398 −0.720165422 0.282014933
[46] −0.790525664 −0.444904551 1.364993169 0.497454338 −0.814396476

Example

 Live Demo

x3<−rnorm(50)
x3

Output

[1] 0.26806584 −0.59220831 2.13348636 1.17274867 0.74676099 −0.23050869
[7] 0.08777170 −2.18373968 −0.46663159 1.68595984 −0.56792093 −0.04674302
[13] −0.15698059 1.60224244 0.76865367 −0.77162936 −0.63068198 −0.83028060
[19] −0.59111274 0.98108541 −0.66160527 −0.77241769 −2.01847347 −0.53358542
[25] 0.43472833 −0.77116734 −0.75394082 −0.29935782 1.66396643 −1.24432984
[31] −0.78313437 0.24483056 −0.14388717 −1.60863142 0.95157997 −1.81913169
[37] 1.78367171 1.88713936 1.49071878 −0.38059952 −0.90937501 −0.33809411
[43] −1.41188352 0.21754289 0.67012617 −0.28785938 0.46930350 −0.47007143
[49] −0.23926592 −0.44746249

Example

 Live Demo

x4<−rnorm(50)
x4

Output

[1] −0.618829657 0.252963051 −0.753368175 0.732276853 −0.402586713
[6] −2.823000119 0.462973827 2.132869726 −0.270486687 0.248525349
[11] 0.038116475 0.394068950 −1.504085198 −1.586890794 −0.927118077
[16] 0.776197040 −0.780684440 −1.278567024 −0.001428128 −1.850978124
[21] 0.451505335 −0.432947055 0.713602899 0.960695470 0.381535210
[26] 1.218072798 −0.017137261 −0.038209493 1.243734395 −0.955858745
[31] 0.915425235 −0.939337976 0.112124820 0.553012619 0.531741963
[36] −0.873762389 −0.186849273 −0.213710488 −0.204011273 1.719709241
[41] 0.202033482 0.512655778 1.452400012 0.363865465 −0.875848946
[46] −0.014560733 −0.724493165 1.969370094 −0.536402427 −0.026232340

Example

 Live Demo

set.seed(101)
x1<−rnorm(50)
x1

Output

[1] −0.3260365 0.5524619 −0.6749438 0.2143595 0.3107692 1.1739663
[7] 0.6187899 −0.1127343 0.9170283 −0.2232594 0.5264481 −0.7948444
[13] 1.4277555 −1.4668197 −0.2366834 −0.1933380 −0.8497547 0.0584655
[19] −0.8176704 −2.0503078 −0.1637557 0.7085221 −0.2679805 −1.4639218
[25] 0.7444358 −1.4103902 0.4670676 −0.1193201 0.4672390 0.4981356
[31] 0.8949372 0.2791520 1.0078658 −2.0731065 1.1898534 −0.7243742
[37] 0.1679838 0.9203352 −1.6716048 0.4484691 0.4824588 0.7582138
[43] −2.3193274 −0.4595048 −1.1053837 0.4029283 0.5689349 −0.7060833
[49] −0.2900906 −1.4838781

Example

 Live Demo

x2<−rnorm(50)
x2

Output

[1] −1.150255281 −0.274471162 0.577901003 −1.396902647 0.749057716
[6] −1.051186697 0.165380871 1.129809120 1.173722464 −0.427863232
[11] −0.259802108 −1.411173044 −0.641357554 0.112457509 0.422604331
[16] 0.386835291 −0.687798326 0.148902489 −0.057649748 −0.074823365
[21] 1.509897438 1.619937008 1.153158167 −0.077603595 −1.818934501
[26] −1.037444583 0.302492246 −1.277946167 0.138339048 −0.050984124
[31] 1.852147575 1.111675270 −0.511375322 −0.543881104 −1.728927284
[36] 0.470749539 0.005387122 1.348045786 0.724096713 1.552549165
[41] 1.325469832 −0.034265092 −0.361013398 −0.720165422 0.282014933
[46] −0.790525664 −0.444904551 1.364993169 0.497454338 −0.814396476

Example

 Live Demo

x3<−rnorm(50)
x3

Output

[1] 0.26806584 −0.59220831 2.13348636 1.17274867 0.74676099 −0.23050869
[7] 0.08777170 −2.18373968 −0.46663159 1.68595984 −0.56792093 −0.04674302
[13] −0.15698059 1.60224244 0.76865367 −0.77162936 −0.63068198 −0.83028060
[19] −0.59111274 0.98108541 −0.66160527 −0.77241769 −2.01847347 −0.53358542
[25] 0.43472833 −0.77116734 −0.75394082 −0.29935782 1.66396643 −1.24432984
[31] −0.78313437 0.24483056 −0.14388717 −1.60863142 0.95157997 −1.81913169
[37] 1.78367171 1.88713936 1.49071878 −0.38059952 −0.90937501 −0.33809411
[43] −1.41188352 0.21754289 0.67012617 −0.28785938 0.46930350 −0.47007143
[49] −0.23926592 −0.44746249

Example

 Live Demo

x_4<−rnorm(50)
x_4

Output

[1] −0.618829657 0.252963051 −0.753368175 0.732276853 −0.402586713
[6] −2.823000119 0.462973827 2.132869726 −0.270486687 0.248525349
[11] 0.038116475 0.394068950 −1.504085198 −1.586890794 −0.927118077
[16] 0.776197040 −0.780684440 −1.278567024 −0.001428128 −1.850978124
[21] 0.451505335 −0.432947055 0.713602899 0.960695470 0.381535210
[26] 1.218072798 −0.017137261 −0.038209493 1.243734395 −0.955858745
[31] 0.915425235 −0.939337976 0.112124820 0.553012619 0.531741963
[36] −0.873762389 −0.186849273 −0.213710488 −0.204011273 1.719709241
[41] 0.202033482 0.512655778 1.452400012 0.363865465 −0.875848946
[46] −0.014560733 −0.724493165 1.969370094 −0.536402427 −0.026232340

Example

 Live Demo

x4<−rnorm(50)
x4

Output

[1] −0.16403235 −1.38327506 0.42351126 −0.79048891 1.20992485 0.89451677
[7] −0.10119854 0.29712257 0.19729772 −0.15698374 1.53657101 −2.16766968
[13] 0.59844815 0.04311236 1.29502719 0.70630294 0.34554508 −0.07989665
[19] 0.45480755 1.27625237 1.26483765 0.26925353 −0.12054409 0.79527135
[25] −0.51402764 −0.40659347 1.21971898 0.08371137 0.58990215 −0.51741928
[31] 0.76946349 0.80196974 −0.69686014 1.17785318 0.58584526 −0.46689388
[37] 0.38564964 −0.53460558 1.05666840 −0.20609327 0.60701224 −0.54806386
[43] −2.09997633 0.25081276 −0.05494528 −0.65972781 −1.45585738 0.02372943
[49] 0.54790809 −0.80890140

Updated on: 08-Feb-2021

768 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements