Can any one help me to solve problem in my code? I am actually trying to find the stopping index N. So first I generate random numbers from normals. There is no problem in finding the first stopping index. Now I want to find the second stopping index using obeservation starting from the one after the first stopping index. E.g. If my first stopping index was 5. I want to set 6th observation from the generated normal variables as the first random number, and I stop at second stopping index. This is my code, alpha <- 0.05 beta <- 0.07 a <- log((1-beta)/alpha) b <- log(beta/(1-alpha)) theta1 <- 2 theta2 <- 3 cumsm<-function(n) {y<-NULL for(i in 1:n) {y[i]=x[i]^2} s=sum(y) return(s) } psum <- function(p,q) {z <- NULL for(l in p:q) { z[l-p+1] <- x[l]^2} ps <- sum(z) return(ps) } smm <- NULL sm <- NULL N <- NULL Nout <- NULL T <- NULL k<-0 x <- rnorm(100,theta1,theta1) for(i in 1:length(x)) { sm[i] <- psum(1,i) T[i] <- ((i/2)*log(theta1/theta2))+(((theta2-theta1)/(2*theta1*theta2))*sm[i])-(i*(theta2-theta1)/2) if (T[i]<=b | T[i]>=a){N[1]<-i break} } for(j in 2:200) { for(k in (N[j-1]+1):length(x)) { smm[k] <- psum((N[j-1]+1),k) T[k] <- ((k/2)*log(theta1/theta2))+(((theta2-theta1)/(2*theta1*theta2))*smm[k])-(k*(theta2-theta1)/2) if (T[k]<=b | T[k]>=a){N[j]<-k break} } } But I cannot get the stopping index after the first one. Tanks -- [[alternative HTML version deleted]]