How to obtain seed after generating random number?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

How to obtain seed after generating random number?

Bogaso
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to obtain seed after generating random number?

r.ookie
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]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to obtain seed after generating random number?

bbolker

> 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 short, I need to know the seed ***after*** generating the random numbers.

  Does ?.Random.seed help?

  It doesn't let you "go back and get the random seed that this sequence
was started from", though.  So you may need to checkpoint reasonably frequently
by saving .Random.seed ...

  Another alternative is to use set.seed() at the same
checkpointing intervals (e.g. a different sequential
value for each in a series of simulations) -- that might run the risk
of a slightly higher correlation between successive simulations (I'm not sure
what can be guaranteed about the properties of sequences from neighboring seeds)
....

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to obtain seed after generating random number?

William Dunlap
In reply to this post by r.ookie
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: [hidden email]
> [mailto:[hidden email]] On Behalf Of r.ookie
> Sent: Tuesday, August 24, 2010 11:48 AM
> To: Bogaso Christofer
> Cc: [hidden email]
> 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]]
>
> ______________________________________________
> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to obtain seed after generating random number?

r.ookie
Thanks for your solution, however, which values would I then provide someone else so that they can produce the same results?

On Aug 24, 2010, at 12:06 PM, William Dunlap wrote:

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: [hidden email]
> [mailto:[hidden email]] On Behalf Of r.ookie
> Sent: Tuesday, August 24, 2010 11:48 AM
> To: Bogaso Christofer
> Cc: [hidden email]
> 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]]
>
> ______________________________________________
> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to obtain seed after generating random number?

William Dunlap
> -----Original Message-----
> From: r.ookie [mailto:[hidden email]]
> Sent: Tuesday, August 24, 2010 12:25 PM
> To: William Dunlap
> Cc: [hidden email]
> Subject: Re: [R] How to obtain seed after generating random number?
>
> Thanks for your solution, however, which values would I then
> provide someone else so that they can produce the same results?

Give them the .Random.seed attribute from the
result of interest, just as I did in the example
of reproducing the result z0.  If they didn't
want to use this function they would have to do
  .Random.seed <<- attr(z0, ".Random.seed")
to set it.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

>
> On Aug 24, 2010, at 12:06 PM, William Dunlap wrote:
>
> 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: [hidden email]
> > [mailto:[hidden email]] On Behalf Of r.ookie
> > Sent: Tuesday, August 24, 2010 11:48 AM
> > To: Bogaso Christofer
> > Cc: [hidden email]
> > 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]]
> >
> > ______________________________________________
> > [hidden email] 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.
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to obtain seed after generating random number?

Greg Snow-2
In reply to this post by Bogaso
If you find yourself doing things like this often, but don't want to explicitly set the seed, or save seeds before simulating, then you can run the following code (or put it into .Rprofile or similar):

.Last.Random.seed <- .Random.seed

addTaskCallback( function(expr, val, ok, visible){
        if(!isTRUE( all.equal(.Last.Random.seed, .Random.seed)) ) {
                .Last.Random.seed <- .Random.seed
        }
        TRUE
        })

Then the previous seed will be stored in .Last.Random.seed and you can restore the seed to be able to rerun the same values, e.g.:

> rnorm(10)
 [1] -0.28361138  0.86951931 -0.54435528  0.62880324 -1.42233446 -1.22751263
 [7] -1.67410552  0.08439848 -0.20612566  1.44187164
> .Random.seed <- .Last.Random.seed
> rnorm(10)
 [1] -0.28361138  0.86951931 -0.54435528  0.62880324 -1.42233446 -1.22751263
 [7] -1.67410552  0.08439848 -0.20612566  1.44187164
> rnorm(10)
 [1] -0.0417821  1.3537545  1.9452253 -0.4909382  0.3884391 -0.8448933
 [7]  0.7379904 -1.0797603 -1.0264739  0.2887934

The above code only keeps the most recent seed, but the code could be modified to store a longer history if that were desired.


If you want to set your own seeds (a little easier to save, pass to others), but find integers to unimaginative, then look at the char2seed function in the TeachingDemos package.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Bogaso Christofer
> Sent: Tuesday, August 24, 2010 11:12 AM
> To: [hidden email]
> Subject: [R] How to obtain seed after generating random number?
>
> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to obtain seed after generating random number?

Paul Gilbert
In reply to this post by Bogaso
It is nearly impossible to go back after the fact and figure out the
seed you started with. So, you need to be careful to record the seed
first. If you are doing your simulations with a function then it is a
good idea to always start in the function by saving a record of the seed
and returning it with the result. Then you always have the information
you need.

Beware that the seed is not the only thing you need to save. You also
need to record the uniform generator you are using, and the generator
for the distribution you are using. Of course, if you always only use
the default then you won't have a problem here, until the default
changes.

You might want to look at the utilities and examples in the package
setRNG.

Paul

>-----Original Message-----
>From: [hidden email]
[mailto:[hidden email]]

>On Behalf Of Bogaso Christofer
>Sent: August 24, 2010 1:12 PM
>To: [hidden email]
>Subject: [R] How to obtain seed after generating random number?
>
>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]]
>
>______________________________________________
>[hidden email] 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.
====================================================================================

La version française suit le texte anglais.

------------------------------------------------------------------------------------

This email may contain privileged and/or confidential information, and the Bank of
Canada does not waive any related rights. Any distribution, use, or copying of this
email or the information it contains by other than the intended recipient is
unauthorized. If you received this email in error please delete it immediately from
your system and notify the sender promptly by email that you have done so.

------------------------------------------------------------------------------------

Le présent courriel peut contenir de l'information privilégiée ou confidentielle.
La Banque du Canada ne renonce pas aux droits qui s'y rapportent. Toute diffusion,
utilisation ou copie de ce courriel ou des renseignements qu'il contient par une
personne autre que le ou les destinataires désignés est interdite. Si vous recevez
ce courriel par erreur, veuillez le supprimer immédiatement et envoyer sans délai à
l'expéditeur un message électronique pour l'aviser que vous avez éliminé de votre
ordinateur toute copie du courriel reçu.
______________________________________________
[hidden email] 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.