Skip to content

Instantly share code, notes, and snippets.

@jameshulse
Created August 1, 2016 15:31
Show Gist options
  • Save jameshulse/f5b85ca038c729a9b772c20390f0d5bc to your computer and use it in GitHub Desktop.
Save jameshulse/f5b85ca038c729a9b772c20390f0d5bc to your computer and use it in GitHub Desktop.
public static class DeterministicGrouping
{
public static bool IsSelected(Guid entropy, ulong numerator, ulong denominator = 100)
{
if(numerator > denominator)
throw new ArgumentOutOfRangeException(nameof(numerator), "The numerator cannot be larger than the denominator.");
if (numerator <= 0)
throw new ArgumentOutOfRangeException(nameof(numerator), "The numerator must be a positive non-zero value.");
if (denominator <= 0)
throw new ArgumentOutOfRangeException(nameof(denominator), "The denominator must be a positive non-zero value.");
ulong guidNumericValue = BitConverter.ToUInt32(entropy.ToByteArray().Take(4).ToArray(), 0);
return guidNumericValue%denominator < numerator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment