lundi 23 janvier 2017

Random Sampling with Distance Condition

I want to sample two integers x and y at random from an interval [1,N] such that |x-y| >= D, for some D < N. The code below (written in R) is what I have been using but it is terribly inefficient. Are there better methods for this sort of sampling? Thanks in adv.

N <- 100; D <- 10;

i <- sample(1:N, 2)

while ( abs( i[1] - i[2] ) < D ){
   i <- sort(sample(1:N, 2))
}




Aucun commentaire:

Enregistrer un commentaire