Last active
December 2, 2016 19:08
-
-
Save PJensen/13e3282a1190080ef73df3da61c16882 to your computer and use it in GitHub Desktop.
Samples the collection using a uniform distribution, do not repeat elements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// SampleService | |
/// </summary> | |
public class SampleService | |
{ | |
private readonly Random _random; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SampleService"/> class. | |
/// </summary> | |
/// <param name="seed">The seed.</param> | |
public SampleService(int seed = 0) | |
{ | |
_random = new Random(seed == 0 ? (int)DateTime.Now.Ticks : seed); | |
} | |
/// <summary> | |
/// Samples the specified universe. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="universe">The universe.</param> | |
/// <param name="sampleSize">Size of the sample.</param> | |
/// <returns></returns> | |
/// <exception cref="System.ArgumentException">sample size is larger than collection</exception> | |
public T[] Sample<T>(IEnumerable<T> universe, int sampleSize) | |
{ | |
var tmp = universe.ToArray(); | |
if (sampleSize > tmp.Length) | |
{ | |
throw new ArgumentException("sample size is larger than collection"); | |
} | |
var tmpReturn = new T[sampleSize]; | |
var usedIndicies = new HashSet<int>(); | |
for (var i = 0; i < sampleSize; i++) | |
{ | |
int sampleIndex; | |
do { sampleIndex = _random.Next(0, tmp.Length); } | |
while (!usedIndicies.Add(sampleIndex)); | |
tmpReturn[i] = tmp[sampleIndex]; | |
} | |
return tmpReturn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment