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.

Revisions

  1. jameshulse created this gist Aug 1, 2016.
    16 changes: 16 additions & 0 deletions DeterministicGrouping.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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;
    }
    }