0

My problem is :

I've already saved a workspace with a fixed simulation that I called X1 from :

alpha=c(0,0.05,0.25,0.5,0.75,0.95,1)
w1=alpha
w2=1-alpha
n=100
m1=2
m2=6
X=matrix (nrow=n,ncol=length(w1))   # on l'appelle X ici mais cela est également notre X1 dans le rapport. 
for (i in 1:length(w1))
   { 
w=sample(c(1,2), size = n, replace = TRUE, prob = c(w1[i],w2[i]))
X[,i]=(w==1)*rnorm(n,m1,1)+(w==2)*rnorm(n,m2,1)         
}    

My problem is that I want to get the seed associated to this particular simulation of X(nrow=100,ncol=7) so that I could re-use it more easily.

I don't know if this could be implemented numerically in R ? And in general ?

  • not sure about R, but I know Python has versions of `random.seed()` that will either seed with the current timestamp (i.e. difficult to recover), or seed with the provided argument, e.g. `random.seed(1)`, so then you know the seed because you passed it in, and can re-seed with `1` to get the same simulation over and over again. Like I said though, not sure if R has something like that, but I think *setting* the seed will be MUCH easier than *recovering* it. – dwanderson Dec 14 '15 at 18:05
  • Thanks for you answer, and sorry for my late response. My question (and my `code`) was badly formulated. My question was actually to know **whether I could retrieve the seed(s) associated to the randomly generated vectors of size n located in the columns of the matrix X, since I saved this matrix in an object but I did not set any seed.** I am thus looking for the opposite : Finding x from `set.seed(x)` applied to generate some random vector (or matrix) X which entries are known. See [an answer here](https://stackoverflow.com/a/19614429/5678813). – Antoine Pissoort Sep 20 '17 at 14:57
  • 1
    Short answer is "no"; technical answer is: you'd need to understand *how* `random` is implemented in R, see how it seeds itself when not provided any seed (some languages use something based on the current timestamp) and then guess in and around that region until you happened to exactly reproduce the matrix you wanted. It's non-trivial. While I doubt R's random is cryptographically secure, the general *point* of random is that it's not easily reversible. If at all possible, I would make a new matrix, after setting, and saving off, the seed. – dwanderson Sep 20 '17 at 18:19

1 Answers1

0

You can use set.seed function just before the for loop. This will make sure that both sample and rnorm produce the same results.

For more info check ?set.seed

Example usage set.seed(123)

To see the actual seed used try .Random.seed

Nishanth
  • 6,932
  • 5
  • 26
  • 38