Skip to content

R-alpha: Treatment of seed

1 message · Arne Kovac

#
There is a difference between R and S (or S-Plus, I'm not sure about
that) concerning the generation of random numbers.

If an error occurs during the execution of a function, the seed is
restored to its old value in S-Plus, but not in R. This feature of S-Plus
has just turned out to be very useful for me because I got an error
message during a small simulation for a certain choice of parameters.

Here is a silly example:

R> silly<-function()
+ {
+ tmp<-rnorm(5)
+ unknownfunction()
+ }
R> .Random.seed
[1]  8886  1153 10190
R> silly()
Error: couldn't find function "unknownfunction"
R> .Random.seed
[1]  9859  1963 19516


Splus> silly<-function()
+ {
+ tmp<-rnorm(5)
+ unknownfunction()
+ }
Splus> .Random.seed
 [1] 37 31 60  6 22  0 47 15  0 30 41  0
Splus> silly()
Error in silly(): couldn't find function "unknownfunction"
Dumped
Splus> .Random.seed
 [1] 37 31 60  6 22  0 47 15  0 30 41  0

Arne Kovac