我想从区间[1,N]中随机采样两个整数x和y,使得| x-y | > = D,对于某些D< N.下面的代码(用R编写)是我一直在使用的,但效率非常低。这种抽样有更好的方法吗?谢谢你。
N <- 100; D <- 10;
i <- sample(1:N, 2)
while ( abs( i[1] - i[2] ) < D ){
i <- sort(sample(1:N, 2))
}
答案 0 :(得分:1)
我想关键是要意识到y依赖于x(或者相反)。这里的算法应该最多可以在三个步骤中起作用:
1. sample x from [1:N]
2. sample y from [1:(x-D)] if (x-D) >= 1
sample y from [x + D:N] if (x+D) <= N
3. If both conditions for y are met, choose one of the generated y uniform at random
这个想法是,一旦x被采样,y需要在[1:(x-D)]或[x + D:N]的范围内,以满足| x-y |。 &gt; = D.
示例:
N = 100; d = 10
a) x is close to N
1. x is sampled from 1:N as 95
2. to satisfy |x-y| >= D, y can be at most 85, so the range to sample y is [1:85]
b) x is close to 1
1. x is sampled from 1:N as 9
2. y must be at least 19, so the range to sample y is [19:N]
c) x is close to 50
1. x is sampled from 1:N as 45
2. y must be either at most 35, or at least 55, so the ranges to sample from are [1:35] and [55:N]
答案 1 :(得分:0)
我会先通过随机抽样得出这个数字之间的差异大于或等于D
。换句话说,我们希望在D
和N-1
之间抽样替换。
difference <- sample(D:(N-1), 20, replace = TRUE)
现在我们需要做的就是选择1
和N - difference
之间的数字来选择较低的数字。我们可以使用vapply
执行此操作。
lowerval <- vapply(N - difference, sample, numeric(1), 1)
最后,我们通过将差值添加到较低值来获得上限值。
upperval <- lowerval + difference