# I grafici man mano che vengono tracciati nascondono i precedenti. Per vedere
# questi devi spostare i nuovi grafici.
#
# w: fraz. del muro aperta; t: tempo tra 2 passaggi; ore: durata del fenomeno
# ifelse(runif(1) < w, 1,0) vale 1 se lo zombie passa per l'apertura
# TZ: variabile indiciata in cui sono classificati i tempi tra i passaggi
sqm <- function(x) sqrt(sum((x-mean(x))^2)/length(x))
w <- 1/10; TZ <- NULL; ore <- 8.5; sec <- 60*60*ore; i <- 1; n <- 0
while (i <= sec)
   { t <- 0
    j <- 0; while(j<1) {j <- ifelse(runif(1) < w,1,0); t <- t+1; i <- i+1}
    n <- n+1; TZ[n] <- t }
summary(TZ); length(TZ)
hist(TZ, right=FALSE)
####
#
# Ecco i due istogrammi
# NZ: variabile indiciata in cui sono messi ogni 60 sec quanti zombie passano
w <- 1/10; TZ <- NULL; ore <- 8.5; sec <- 60*60*ore; i <- 1; n <- 0
NZ <- NULL; u <- 0
while (i <= sec)
   { t <- 0
    j <- 0; while(j<1) {j <- ifelse(runif(1) < w,1,0); t <- t+1; r <- i/60; u <- u+j
                        if(r==floor(r)){NZ[r] <- u; u <- 0}
                        i <- i+1}
    n <- n+1; TZ[n] <- t }
summary(TZ); length(TZ)
hist(TZ, right=FALSE)
dev.new(3.5,3.5)
summary(NZ); length(NZ)
# e' una variabile ad uscite intere; centro le colonne negli interi
m <- max(NZ); hist(NZ,seq(-1/2,m+1/2,1), right=FALSE)
####
#
# I due istogrammi per piu' uscite
w <- 1/10; TZ <- NULL; ore <- 48; sec <- 60*60*ore; i <- 1; n <- 0
NZ <- NULL; u <- 0
while (i <= sec)
   { t <- 0
    j <- 0; while(j<1) {j <- ifelse(runif(1) < w,1,0); t <- t+1; r <- i/60; u <- u+j
                        if(r==floor(r)){NZ[r] <- u; u <- 0}
                        i <- i+1}
    n <- n+1; TZ[n] <- t }
dev.new(3.5,3.5)
summary(TZ); length(TZ); sqm(TZ)
hist(TZ, right=FALSE, probability=TRUE,col="yellow")
abline(h=seq(0.01,0.07,0.01),lty=3)
dev.new(3.5,3.5)
summary(NZ); length(NZ); sqm(NZ)
m <- max(NZ)
hist(NZ,seq(-1/2,m+1/2,1),right=FALSE,probability=TRUE,col="yellow")
abline(h=seq(0.02,0.16,0.02),lty=3)