[R] set.seed

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Wed Jun 14 11:57:20 CEST 2006


ronggui wrote:
> set.seed is used to set the random number seed.
> When we use functions ,say runif, to generate random number ,we almost
> get different set of random number.

> As for what the i in set.seed(i) should be,I don't think it is a serious 
> matter.


The help for set.seed tells you all you need to know. 'i' must be a 
single value "interpreted as an integer".

  You can give it a decimal number, but it makes it an integer:

 > set.seed(pi)
 > runif(2)
[1] 0.1680415 0.8075164
 > set.seed(3)
 > runif(2)
[1] 0.1680415 0.8075164

  But not too big an 'integer':

  > set.seed(1e100)
  Error in set.seed(1e+100) : supplied seed is not a valid integer
  In addition: Warning message:
  NAs introduced by coercion

  because 1e100 isn't represented as an integer internally (in C/Fortran 
code, its a 'float' or'double precision' type of thing.

  For me it takes signed 32 bit integers, so the limits are +/- 2147483647:

  > set.seed(2147483647)
  > set.seed(-2147483647)
  > set.seed(-2147483648)
  Error in set.seed(-2147483648) : supplied seed is not a valid integer
  In addition: Warning message:
  NAs introduced by coercion
  > set.seed(2147483648)
  Error in set.seed(2147483648) : supplied seed is not a valid integer
  In addition: Warning message:
  NAs introduced by coercion

A 32 bit integer gives you over 4 billion possible random sequences. Is 
that enough?

Barry



More information about the R-help mailing list