t = 100000 
success.r = rep(0,t) 
success.o = rep(0,t) 


for(i in 1:t){
  escape = rep(F,100)
  ticket = sample(1:100)
  for(j in 1:length(prisoner)){
    escape[j] = j%in% sample(ticket,50)
  }
  success.r[i] = sum(escape)
}


for(i in 1:t){
  escape = rep(F,100)
  ticket = sample(1:100)
  for(j in 1:100){
    boxes = 0
    current.box = j
    while(boxes<50 &&!escape[j]){
      boxes=boxes+1
      escape[j] = ticket[current.box]==j
      current.box = ticket[current.box]
    }
  }
  success.o[i] = sum(escape)
}

cat("Random method resulted in a success rate of ",100*mean(success.r==100),
    "%.\nOptimal method resulted in a success rate of ",100*mean(success.o==100),"%.",sep="")