2

I am trying to get reproducible results. I assign random numbers to each entry 1 to 100 in a data frame. How do I get the same number from a random number generator?

seed <- 1

seed = as.integer(Sys.time())
set.seed(seed)


ID <- 1:100
x <- as.integer(rnorm(100,50,20))

y <- as.integer(rnorm(100,50,20)) 

agent <- as.matrix(cbind(ID,x,y))

This is how I tried it without success.

ayhan
  • 70,170
  • 20
  • 182
  • 203
S Front
  • 333
  • 1
  • 8
  • 3
    You will get the same results if you set `seed` as the same value immediately before generating the random numbers. But your second line sets it to a different value each time you run it. – Andrew Gustar Jun 05 '18 at 13:55
  • 2
    You could [set the seed multiple times](https://stackoverflow.com/questions/45324208/weird-behavior-of-using-set-seed-multiple-times/45324286#45324286), although here just `as.matrix(cbind(ID,x,x))` could also work? – Florian Jun 05 '18 at 13:55
  • Please see the answer to this question: https://stackoverflow.com/questions/14380096/set-seed-with-r-2-15-2 – Dylan Smith Jun 05 '18 at 13:56
  • Thanks everyone So when I put ID <- 1:100 set.seed(12345) x <- as.integer(rnorm(100,50,20)) set.seed(12345) y <- as.integer(rnorm(100,50,20)) agent <- as.matrix(cbind(ID,x,y)) I always get the same results for agent – S Front Jun 05 '18 at 14:05

0 Answers0