Resample
Select elements from sample with replacement using generator .
- Algorithm — uniform sampling with replacement, see Resample
- Complexity — time
- Output — new array with elements (may contain duplicates)
- Domain — , sample size
Notation
- sample ()
- individual measurements
Properties
- Independence each selection is independent with equal probability
- 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 replacementr.Resample(x, n)— bootstrap sample of same size as original
Implementation names
| Language | Method |
|---|---|
| C# | Rng.Resample() |
| Go | Resample() |
| Kotlin | Rng.resample() |
| Rust | Rng::resample() |
| Python | Rng.resample() |
| R | rng$resample() |
| TypeScript | Rng.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 (without replacement), 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 function selects elements from a sample of size with replacement.
The algorithm generates independent uniform random integers in 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 for every element. The same element may appear multiple times in the output. Time complexity is with additional space for the result array.
Tests
The test suite contains 19 test cases validating sampling with replacement (bootstrap resampling). Given a seed, input array of size , and draw count , returns elements drawn independently and uniformly from (with replacement, so duplicates are possible). All tests verify reproducibility: the same seed, input, and must produce the same output across all language implementations.
Seed variation 6 tests with different seeds:
seed-0-n10-k3: seed , ,seed-42-n10-k5: seed , ,seed-123-n10-k3: seed , ,seed-314-n10-k10: seed , ,seed-999-n10-k3: seed , ,seed-2718-n100-k25: seed , ,
These tests validate that different seeds produce different bootstrap samples from the same input.
Parameter variation (seed ) 13 tests exploring and :
seed-1729-n1-k1: , (trivial case)seed-1729-n2-k1: , (single draw from two)seed-1729-n5-k3: , (standard draw)seed-1729-n5-k7: , (, valid for resampling)seed-1729-n10-k1: , (single draw from many)seed-1729-n10-k3: , (standard draw)seed-1729-n10-k5: , (half draw)seed-1729-n10-k10: , (draw equal to pool size)seed-1729-n10-k15: , (, exercises repeated sampling)seed-1729-n20-k5: ,seed-1729-n20-k10: ,seed-1729-n100-k10: , (large pool, small draw)seed-1729-n100-k25: , (large pool, moderate draw)
Unlike (without replacement), allows since each draw is independent. The cases (n5-k7, n10-k15) are unique to resampling and validate that the output can contain repeated values from the input.