JoshDG
JoshDG

Reputation: 3931

Same Random Numbers Every Time

I'm running a script from the command line via R CMD BATCH script.in.R script.out.R &

I have the following line, which picks 12 random row ids and sorts them:

test.index<-sort(sample(1:nrow(recoded),12))

It spits out the same 12 numbers every time if I don't change the script. If I change it a little bit (change a label or a string or anything) then the numbers are different...I need them to be different every time!

Any ideas?

Upvotes: 4

Views: 7752

Answers (4)

Yes, I had the same problem and it was because I was loading a Workspace in which the seed had been set. The way to get it back to normal is...

set.seed(Null)

note that setting the seed to something random will not work, since the random number generated will always be the same, and therefore the new seed. But the above works.

Upvotes: 0

user7603640
user7603640

Reputation: 1

Check if you have not loaded any previous workspace. If you have, the previous seed is also loaded, thus giving you the same results.

Upvotes: 0

Tommy
Tommy

Reputation: 40803

This sounds weird. What's the rest of the script doing? If it calls (or some other function it calls) set.seed, that would explain things, but since you say changing (what I assume to be) the data, that would imply that the seed is set to some hash of your dataset?! Or is it if you change the script in any way?

Anyway, you can insert a line like rm(.Random.seed, envir=globalenv()) before your call to sample, which should reset the seed to a random one...

Another way is to generate a unique seed yourself. Here's one way based on time and process id.

 set.seed( as.integer((as.double(Sys.time())*1000+Sys.getpid()) %% 2^31) )

Upvotes: 20

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

You probably have a call to set.seed() in there. Here is an example:

$ Rscript -e 'runif(4)'      
[1] 0.639716 0.976892 0.486573 0.525979

$ Rscript -e 'runif(4)'
[1] 0.516927 0.951013 0.931756 0.741650

$ Rscript -e 'runif(4)'
[1] 0.159682 0.314798 0.356476 0.584326

$ Rscript -e 'set.seed(42); runif(4)'
[1] 0.914806 0.937075 0.286140 0.830448

$ Rscript -e 'set.seed(42); runif(4)'
[1] 0.914806 0.937075 0.286140 0.830448

$ 

The first three all differ, then I enforce a common seed and presto the numbers are identical.

Also, Rscript is nicer than R CMD BATCH.

Upvotes: 6

Related Questions