## Suppose that it takes 15 seconds to cross the street, and the ## probability that no car passes in any five second interval is a ## given probability p. Calculate the average time it takes to cross ## the street, and the probability that it takes longer than one ## minute to cross. ## The probability that no car passes in any five second interval. p = 0.7 ## The number of simulation replications. nrep = 1e3 ## The total of all waiting times (including the 15 seconds required ## to cross). WT = 0 ## The number of occasions on which more than 60 seconds was required to ## cross (including the 15 seconds that it takes to cross). T60 = 0 ## Simulation replications. for (k in 1:nrep) { ## Indicates when cars are present for the first three time blocks. C = 1*(runif(3) < p) ## Determine how long it takes to cross in this simulation. n = 3 while (TRUE) { ## Check whether we can cross. if (all(C)) { break } ## Update the status of the cars. C = c(C[2], C[3], 1*(runif(1)12) } ## Convert to a mean and a probability. WT = WT/nrep T60 = T60/nrep