# Simulazione del modello di Ehrenfest (1907). nA <- 1 nB <- 100 n <- nA + nB if (n%%2!=0) { nB <- nB + 1 n <- n + 1 warning("Ho messo una pallina in piu' nell'urna B!") } A <- c(1:nA) B <- c((nA+1):n) nest <- 0 repeat { out <- sample(c(A,B),size=1) nest <- nest + 1 ris <- intersect(out,A) if (length(ris)==1) { A <- setdiff(A,out) B <- c(B,out) } else { B <- setdiff(B,out) A <- c(A,out) } if (length(A)==length(B)) break } cat("Esperimento terminato dopo",nest,"estrazioni\n") # una variante per la simulazione del modello di Ehrenfest. nA <- 1 nB <- 9999 n <- nA + nB if (n%%2!=0) { nB <- nB + 1 n <- n + 1 warning("Ho messo una biglia in piu' nell'urna B!") } A <- c(1:nA) B <- c((nA+1):n) nmax <- 100000 nbig <- rep(NA,nmax) nest <- 0 repeat { out <- sample(c(A,B),size=1) nest <- nest + 1 ris <- intersect(out,A) if (length(ris)==1) { A <- setdiff(A,out) B <- c(B,out) } else { B <- setdiff(B,out) A <- c(A,out) } nbig[nest] <- length(A) if (length(A)==length(B)) break } ok <- !is.na(nbig) nbig <- nbig[ok] plot(nbig,type="l") abline(h=n/2,lty=2)