Monday, October 8, 2012


Weighted Random Sampling With Replacement 


Select k items from n with replacement, based on weights w[n]. Populate dest from src. Here, k=n


double sum_w = 0.0;
for(i = 0; i < n; ++i) {
  sum_w += w[i];
}

int i, j;

for (j = 0; j < n; ++j){
  double u = random*sum_w;
  for(i = 0; i < n; ++i) {
    u -= w[i];
    if (u < 0) {break;}
  }
  dest[j]=src[i];
}


No comments: