# ----------------------------------------------------------------------- urna <- c(rep("Bianca",7),rep("Nera",3)) nrep <- 1000 out <- rep(NA,nrep) for (i in 1:nrep) { x <- sample(urna,4,replace=FALSE) y <- sum(x == "Bianca") out[i] <- y } table(out) # ----------------------------------------------------------------------- # ----------------------------------------------------------------------- moneta <- c("TESTA","CROCE") nrep <- 100000 st.1 <- system.time(x <- sample(moneta,nrep,replace=TRUE)) st.2 <- system.time( { x <- rep(NA,nrep) for (i in 1:nrep) x[i] <- sample(moneta,1) } ) st.2[3] - st.1[3] st.2[3] / st.1[3] # -----------------------------------------------------------------------