What is the use of the ".Random.Seed" vector?

Asked

Viewed 186 times

5

I realized that in the r there is a hidden vector in the globalenv(), called .Random.seed. Even with the globalenv() with no object, .Random.seed is still available when using the function ls with the parameter TRUE in the argument all:

ls(all=FALSE)
#character(0)

ls(all=TRUE)
[1] ".Random.seed"

And in trying to eliminate him:

rm(list=ls())

it is still available because the length of globalenv() has 1 as a result:

length(globalenv())
[1] 1

Thus,

  • what is the utility of the vector .Random.seed?
  • how to eliminate this vector? And, what implications can occur if it is removed?
  • 1

    What a breeze. Good question!

  • 2

    But you can delete yes. Enough rm(list = ls(all.names = TRUE)). After that, ls(all.names = TRUE) results in character(0).

  • 1

    Another interesting question is why he’s in .GlobalEnv. Other hidden objects such as .Library stay in the base, for example.

  • I edited the question based on your comment.

2 answers

4


.Random.Seed is an integer vector, containing the state of the random number generator (RNG) for the generation of random numbers in R. It can be saved and restored, but should not be changed by the user.

It is applied in functions such as rnorm, rexp, ..., that generate random numbers in certain distributions. So if you are not using random numbers, then you should have no problems.

1

I will complement the reply given by @Márcio Mocellin on the second topic:

how to eliminate this object?

As quoted by @Tomás Barcellos in the comments:

rm(list = ls(all.names = TRUE))
ls(all.names = TRUE)

And, what implications may occur if removed?

I did two tests to verify the existence of possible problems regarding this:

  • first, I eliminated .Random.seed;

  • second, I applied two functions that generate and define random number patterns (I applied sample and set.seed, respectively).

sample worked properly. And, interestingly, .Random.seed was activated, returning to globalenv(). To prove this:

#Eliminar .Random.seed do globalenv()
rm(list = ls(all.names = TRUE))
ls(all.names = TRUE)
#character(0)

#Aplicação da função
x<-sample(1:10,10,replace=T)
#[1] 44 39 27 17 10 17 49 49  5 21

#Reaparecimento da função .Random.seed ao globalenv()
ls(all.names = TRUE)
#[1] ".Random.seed" "x"

When using the function set.seed, .Random.seed is also activated and returns to globalenv():

#Eliminar .Random.seed do globalenv()
rm(list = ls(all.names = TRUE))
ls(all.names = TRUE)
#character(0)

#Aplicação da função
set.seed(456)

#Reaparecimento da função .Random.seed ao globalenv()
ls(all.names = TRUE)
[1] ".Random.seed"
  • This was an empirical, test-based response. I cannot say if there is any situation in which the removal of .Random.seed is harmful. Only theory can say this. Unfortunately, I found nothing (reliable) about.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.