Last active
November 22, 2017 18:47
-
-
Save rossipedia/13595a592bd3387c412c711261b215cb to your computer and use it in GitHub Desktop.
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
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
on my machine (i7 7700) worst case averages out to about 17 ticks for
max
of 64