pick a random number, always with increasing value over last random number picked
This uses a while
loop and is wrapped in a function
# from ?sampleresample <- function(x, ...) x[sample.int(length(x), ...)]sample_z <- function(n){ z <- numeric(n) new <- 0 count <- 1 while(new < n){ from <- seq(new+1,n,by=1) new <- resample(from, size= 1) z[count] <- new if(new < n) count <- count+1 } z[1:count]}set.seed(1234)sample_z(100)## [1] 12 67 88 96 100
Edit
note the change to deal with when the new sample is 100 and the way sample
deals with an integer as opposed to a vector for x
Edit 2
Actually reading the help for sample
gave the useful resample
function. Which avoids the pitfalls when length(x) == 1
Not particularly efficient but:
X <- 0samps <- c()while (X < 100) { if(is.null(samps)) {z <- 1 } else {z <- 1 + samps[length(samps)]} if (z == 100) { samps <- c(samps, z) } else { samps <- c(samps, sample(z:100, 1)) } X <- samps[length(samps)]}
sampsEDIT: Trimming a little fat from it:
samps <- c()while (is.null(samps[length(samps)]) || samps[length(samps)] < 100 ) { if(is.null(samps)) {z <- 1 } else {z <- 1 + samps[length(samps)]} if (z == 100) { samps <- c(samps, z) } else { samps <- c(samps, sample(z:100, 1)) }}samps