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

Greg Snow Greg.Snow at imail.org
Wed Aug 25 19:28:55 CEST 2010


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.



More information about the R-help mailing list