-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReservoirSampling.cs
44 lines (36 loc) · 1.39 KB
/
ReservoirSampling.cs
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
35
36
37
38
39
40
41
42
43
44
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;
}
}
}