MCPcopy Create free account
hub / github.com/RcppCore/Rcpp / EmpiricalSample

Function EmpiricalSample

inst/include/Rcpp/sugar/functions/sample.h:337–363  ·  view source on GitHub ↗

Adapted from segment of `do_sample` Index version

Source from the content-addressed store, hash-verified

335// Adapted from segment of `do_sample`
336// Index version
337inline Vector<INTSXP> EmpiricalSample(int n, int size, bool replace, bool one_based)
338{
339 Vector<INTSXP> ans = no_init(size);
340 Vector<INTSXP>::iterator ians = ans.begin(), eans = ans.end();
341
342 int adj = one_based ? 1 : 0;
343
344 if (replace || size < 2) {
345 for ( ; ians != eans; ++ians) {
346 *ians = static_cast<int>(n * unif_rand() + adj);
347 }
348 return ans;
349 }
350
351 IntegerVector x = no_init(n);
352 for (int i = 0; i < n; i++) {
353 x[i] = i;
354 }
355
356 for ( ; ians != eans; ++ians) {
357 int j = static_cast<int>(n * unif_rand());
358 *ians = x[j] + adj;
359 x[j] = x[--n];
360 }
361
362 return ans;
363}
364
365// Element version
366template <int RTYPE>

Callers 1

sampleFunction · 0.85

Calls 5

no_initFunction · 0.85
unif_randFunction · 0.85
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected