生成间隔至少 10 的随机数

bot*_*tom 5 random numbers r

我想生成 1 到 100,000 之间的 100 个数字,每个数字之间的空格至少为 10。

一种方法是将 100,000 除以 10,然后进行采样(1000,100)并乘以 10 得到答案,但数字均以 0 结尾。

如何生成不以 0 结尾的随机数?

Mic*_*ico 2

我无法想出一种非递归的方法来做到这一点(第一个想法是在你的方法中添加“抖动”,但这几乎肯定会使一些观察结果彼此过于接近),但这会起作用:

set.seed(102438)
include <- 1:100000
smpl <- integer(100)

for (i in 1:length(smpl)){
  smpl[i] <- si <- sample(include, 1)
  #now remove anything within 10 of the current draw
  include <- setdiff(include, (si - 10L):(si + 10L))
}

min(abs(diff(smpl)))
# [1] 1105
Run Code Online (Sandbox Code Playgroud)