Bogaso Christofer
2010-Aug-24 17:11 UTC
[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]]
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.
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 greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Bogaso Christofer > Sent: Tuesday, August 24, 2010 11:12 AM > To: r-help at r-project.org > 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]] > > ______________________________________________ > 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.
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: r-help-bounces at r-project.org[mailto:r-help-bounces at r-project.org]>On Behalf Of Bogaso Christofer >Sent: August 24, 2010 1:12 PM >To: r-help at r-project.org >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 forsome>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.=================================================================================== 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.