Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same Random Data generated when .RData is loaded

Tags:

r

When a .RData file is loaded same random numbers are generated everytime. For example try this: (Type these in terminal)

rm(list=ls())
x=10 #Just some random value 
save.image("samplefile.RData")

Now try this:
rm(list=ls())
load("samplefile.RData")
print(runif(n=100,min=0,max=100)) #Now it prints same random numbers everytime i run above code junket.

Can anyone please explain?

Thanks.
like image 464
Kashyap Nagaraja Avatar asked Oct 17 '22 18:10

Kashyap Nagaraja


2 Answers

This is intentional behaviour - .Random.seed is saved within Rdata file. If you want different data generated just rm() the value before that or set it to a different one.

like image 118
Martin Lhotsky Avatar answered Oct 20 '22 10:10

Martin Lhotsky


If you need to load a .RData file that has a saved .Random.seed, you can reset the seed using the clock time and this bit of code:

a <- as.numeric(Sys.time())
set.seed(a)

Note that there are benefits to being able to exactly reproduce randomizations, i.e., reproducible research. But for everyday purposes it's probably safer to save and load objects rather than the environment. https://www.rdocumentation.org/packages/base/versions/3.4.0/topics/readRDS

like image 33
klocey Avatar answered Oct 20 '22 11:10

klocey