Adapted from segment of `do_sample` Index version
| 335 | // Adapted from segment of `do_sample` |
| 336 | // Index version |
| 337 | inline 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 |
| 366 | template <int RTYPE> |