vendredi 12 juin 2015

Cannot understand random number algrothms give different results

I'm trying to understand why the following 2 code snippets leads to different output and I'm hoping someone can enlighten me. The second version is marginally faster - which is my motivation since this all sits in a loop, but I need to know if it is equivalent and if not then why ?

    things <- 100000
    n.events <- 3
    probs <- rep(0.05, n.events)

    # Method 1
    set.seed(1)
    events1 <- matrix(n.events, nrow=things)
    events1 <- sapply(probs, function(probs) rbinom(things,1,probs))

    # Method 2
    set.seed(1)
    events2 <- matrix( rbinom(things*n.events, 1, probs), ncol=n.events,    byrow=TRUE)

    > identical(events1, events2)
    [1] FALSE




Aucun commentaire:

Enregistrer un commentaire