-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReservoirSampling.cs
More file actions
34 lines (25 loc) · 1.15 KB
/
ReservoirSampling.cs
File metadata and controls
34 lines (25 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
namespace AlgorithmsAndDataStructures.Algorithms.Sampling;
public class ReservoirSampling
{
#pragma warning disable CA1822 // Mark members as static
public int[] GetReservoirSample(SampleSource population, int sampleSize)
#pragma warning restore CA1822 // Mark members as static
{
if (population is null) return Array.Empty<int>();
var reservoir = new int[sampleSize];
var random = new Random();
// We populate all samples from population to result right-away because we don't know whether we get sampleSize + 1 at any time at all.
for (var i = 0; i < sampleSize; i++) reservoir[i] = population.GetNext();
var currentPopulationSize = sampleSize;
var nextSample = population.GetNext();
while (nextSample > 0)
{
currentPopulationSize++;
var probabilityToPickNextSample = random.Next(1, currentPopulationSize);
if (probabilityToPickNextSample <= sampleSize) reservoir[probabilityToPickNextSample - 1] = nextSample;
nextSample = population.GetNext();
}
return reservoir;
}
}