Resample

r.Resample(x,k)r.\operatorname{Resample}(\mathbf{x}, k)

Select kk elements from sample x\mathbf{x} with replacement using generator rr.

  • Algorithm — uniform sampling with replacement, see Resample
  • ComplexityO(k)O(k) time
  • Output — new array with kk elements (may contain duplicates)
  • Domaink0k \geq 0, sample size n1n \geq 1

Notation

  • x=(x1,,xn)\mathbf{x} = (x_1, \ldots, x_n) sample (n1n \geq 1)
  • xix_i individual measurements

Properties

  • Independence each selection is independent with equal probability 1n\frac{1}{n}
  • Duplicates same element may appear multiple times in output
  • Determinism same generator state produces same selection

Example

  • Rng("demo-resample").Resample([1, 2, 3, 4, 5], 3) — select 3 with replacement
  • r.Resample(x, n) — bootstrap sample of same size as original

Implementation names

LanguageMethod
C#Rng.Resample()
GoResample()
KotlinRng.resample()
RustRng::resample()
PythonRng.resample()
Rrng$resample()
TypeScriptRng.resample()

Resample\operatorname{Resample} picks elements with replacement, allowing the same element to be selected multiple times. This is essential for bootstrap methods where we simulate new samples from the observed data. Unlike Sample\operatorname{Sample} (without replacement), Resample\operatorname{Resample} can produce outputs larger than the input and will typically contain duplicate values. For reproducible bootstrap, combine with a seeded generator: Resample(data, n, Rng("bootstrap-1")).

Algorithm

The Resample\operatorname{Resample} function selects kk elements from a sample of size nn with replacement.

The algorithm generates kk independent uniform random integers in [0,n)[0, n) using the Rng generator, and collects the corresponding elements:

result = new array of size k
for i from 0 to k-1:
   j = uniform_int(0, n)
   result[i] = x[j]

Each selection is independent with equal probability 1n\frac{1}{n} for every element. The same element may appear multiple times in the output. Time complexity is O(k)O(k) with O(k)O(k) additional space for the result array.

Tests

Resample(seed,x,k)\operatorname{Resample}(\text{seed}, \mathbf{x}, k)

The Resample\operatorname{Resample} test suite contains 19 test cases validating sampling with replacement (bootstrap resampling). Given a seed, input array x\mathbf{x} of size nn, and draw count kk, Resample\operatorname{Resample} returns kk elements drawn independently and uniformly from x\mathbf{x} (with replacement, so duplicates are possible). All tests verify reproducibility: the same seed, input, and kk must produce the same output across all language implementations.

Seed variation 6 tests with different seeds:

  • seed-0-n10-k3: seed =0= 0, n=10n = 10, k=3k = 3
  • seed-42-n10-k5: seed =42= 42, n=10n = 10, k=5k = 5
  • seed-123-n10-k3: seed =123= 123, n=10n = 10, k=3k = 3
  • seed-314-n10-k10: seed =314= 314, n=10n = 10, k=10k = 10
  • seed-999-n10-k3: seed =999= 999, n=10n = 10, k=3k = 3
  • seed-2718-n100-k25: seed =2718= 2718, n=100n = 100, k=25k = 25

These tests validate that different seeds produce different bootstrap samples from the same input.

Parameter variation (seed =1729= 1729) 13 tests exploring nn and kk:

  • seed-1729-n1-k1: n=1n = 1, k=1k = 1 (trivial case)
  • seed-1729-n2-k1: n=2n = 2, k=1k = 1 (single draw from two)
  • seed-1729-n5-k3: n=5n = 5, k=3k = 3 (standard draw)
  • seed-1729-n5-k7: n=5n = 5, k=7k = 7 (k>nk > n, valid for resampling)
  • seed-1729-n10-k1: n=10n = 10, k=1k = 1 (single draw from many)
  • seed-1729-n10-k3: n=10n = 10, k=3k = 3 (standard draw)
  • seed-1729-n10-k5: n=10n = 10, k=5k = 5 (half draw)
  • seed-1729-n10-k10: n=10n = 10, k=10k = 10 (draw equal to pool size)
  • seed-1729-n10-k15: n=10n = 10, k=15k = 15 (k>nk > n, exercises repeated sampling)
  • seed-1729-n20-k5: n=20n = 20, k=5k = 5
  • seed-1729-n20-k10: n=20n = 20, k=10k = 10
  • seed-1729-n100-k10: n=100n = 100, k=10k = 10 (large pool, small draw)
  • seed-1729-n100-k25: n=100n = 100, k=25k = 25 (large pool, moderate draw)

Unlike Sample\operatorname{Sample} (without replacement), Resample\operatorname{Resample} allows k>nk > n since each draw is independent. The k>nk > n cases (n5-k7, n10-k15) are unique to resampling and validate that the output can contain repeated values from the input.

References

Bootstrap Methods: Another Look at the Jackknife
Efron, Bradley (1979)
The Annals of Statistics