一种方法是从您抽取样本的总体中删除候选人:
set.seed(42)
population <- 1:356
n_samples <- 5
window <- 14
sampled_session <- rep(sample(population, 1), n_samples) # initialize the vector
for (i in seq.int(2, n_samples)) {
borders <- sampled_session[i - 1] + (window - 1) * c(-1, 1)
days_in_window <- seq.int(borders[1], borders[2])
population <- setdiff(population, days_in_window)
sampled_session[i] <- sample(population, 1)
}
sort(sampled_session)
# [1] 90 193 264 309 326
diff(sort(sampled_session))
# [1] 103 71 45 17
另一种方法是
set.seed(357)
population <- 1:357
n_samples <- 5
window <- 14
sampled.session <- numeric(n_samples)
for (i in seq_len(n_samples)) {
sampled.session[i] <- pick <- sample(population, 1)
population <- population[-which(population < pick + window & population > pick - window)]
}
sort(sampled.session)
[1] 19 39 111 134 267