-1

I am trying to generate random numbers for a simulation (the example below uses the uniform distribution for simplicity). Why would these two methods produce different average values (a: 503.2999, b: 497.5372) when sampled 10k times with the same seed number:

set.seed(2)

a <- runif(10000, 1, 999)

draw <- function(x) {
  runif(1, 1, 999)
}

b <- sapply(1:10000, draw)
print(c(mean(a), mean(b)))

In my model, the random number for the first method would be referenced within a simulation using a[sim_number] while in the second instance, the runif function would be placed inside the simulation function itself. Is there a correct way of doing it?

ToroJ
  • 15
  • 4

1 Answers1

0

For completeness, the answer is that you need to set the seed before each random draw if you want them to be the same.

joran
  • 169,992
  • 32
  • 429
  • 468