[R] How to obtain seed after generating random number?

William Dunlap wdunlap at tibco.com
Tue Aug 24 21:06:34 CEST 2010


The following will attach (as an attribute) the current global value
of .Random.seed to the value of the evaluated 'expr' argument.  If you
supply
the initial.Random.seed argument then it will use that when evaluating
the expression (and also attach it to the result) so you can repeat the
'unusual' computation.  I sometimes use this sort of thing when doing QA
work.
    f <- function(expr, initial.Random.seed) {
        if (missing(initial.Random.seed)) {
             if (!exists(".Random.seed", envir=.GlobalEnv)) {
                 runif(1) # force generation of a .Random.seed
             }
             initial.Random.seed <- get(".Random.seed",
envir=.GlobalEnv)
        }
        .Random.seed <<- initial.Random.seed
        structure(expr, .Random.seed=.Random.seed)
    }

E.g.,
> z0 <- f(runif(3))
> str(z0)
 atomic [1:3] 0.15 0.225 0.607
 - attr(*, ".Random.seed")= int [1:626] 403 19 1644829386 1412664364
-3288017 -689767195 792688028 -702547982 -676502931 402532263 ...
> z1 <- f(runif(3))
> str(z1)
 atomic [1:3] 0.786 0.325 0.789
 - attr(*, ".Random.seed")= int [1:626] 403 22 1644829386 1412664364
-3288017 -689767195 792688028 -702547982 -676502931 402532263 ...
> str(f(runif(3), attr(z0,".Random.seed"))) # repeat with .Random.seed
used to make z0
 atomic [1:3] 0.15 0.225 0.607
 - attr(*, ".Random.seed")= int [1:626] 403 19 1644829386 1412664364
-3288017 -689767195 792688028 -702547982 -676502931 402532263 ...

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of r.ookie
> Sent: Tuesday, August 24, 2010 11:48 AM
> To: Bogaso Christofer
> Cc: r-help at r-project.org
> Subject: Re: [R] How to obtain seed after generating random number?
> 
> I have wondered this in the past too so thanks for the question.
> 
> On Aug 24, 2010, at 10:11 AM, Bogaso Christofer wrote:
> 
> Dear all, I was doing an experiment to disprove some theory therefore
> performing lot of random simulation. Goal is to show the audience that
> although something has very rare chance to occur but it 
> doesn't mean that
> event would be impossible.
> 
> 
> 
> In this case after getting that rare event I need to show 
> that same scenario
> for multiple times to explain other audience. Hence I need to 
> somehow save
> that seed which generates that random numbers after doing the 
> experiment.
> However as it is very rare event it is not very practical to 
> start with a
> fixed seed and then generate random numbers. Hence I am 
> looking for some way
> which will tell me about that corresponding seed which was 
> responsible to
> generate that particular series of random numbers responsible 
> for occurrence
> of that rare event.
> 
> 
> 
> In short, I need to know the seed ***after*** generating the 
> random numbers.
> 
> 
> 
> Is there any possibility to know this?
> 
> 
> 
> Thanks and regards,
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list