lundi 18 avril 2016

Correct way of drawing random number for a simulation

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?




Aucun commentaire:

Enregistrer un commentaire