Skip to content

Instantly share code, notes, and snippets.

@rossipedia
Last active November 22, 2017 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossipedia/13595a592bd3387c412c711261b215cb to your computer and use it in GitHub Desktop.
Save rossipedia/13595a592bd3387c412c711261b215cb to your computer and use it in GitHub Desktop.
struct MiniRandomSequence
{
public MiniRandomSequence(byte max, Random rng)
{
if (max > 64)
throw new ArgumentOutOfRangeException($"MiniRandomSequence max range is 63 (got {max})");
_rng = rng;
_max = max;
_seen = 0;
_done = max < 64 ? ((1L << max) - 1) : -1L;
_value = 0; //
}
byte _max;
byte _value;
long _seen;
readonly long _done;
readonly Random _rng;
public MiniRandomSequence GetEnumerator()
{
_seen = 0;
return this;
}
public int Current => _value;
public bool MoveNext()
{
if (_seen == _done)
return false;
while (true)
{
_value = (byte) _rng.Next(_max);
if ((_seen & (1L << _value)) != 0)
continue;
_seen |= 1L << _value;
return true;
}
}
}
@rossipedia
Copy link
Author

on my machine (i7 7700) worst case averages out to about 17 ticks for max of 64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment