Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created October 6, 2017 02:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brianmfear/0694dc01948b07afcb7fa1da0de4a656 to your computer and use it in GitHub Desktop.
Save brianmfear/0694dc01948b07afcb7fa1da0de4a656 to your computer and use it in GitHub Desktop.
namespace NumberGenerators {
public class UniqueNumberGenerator
{
int value;
public UniqueNumberGenerator(int value)
{
this.value = value;
}
public Nullable<int> NextId()
{
value++;
int a = (value >> 25) & 31, b = (value >> 20) & 31, c = (value >> 15) & 31,
d = (value >> 10) & 31, e = (value >> 5) & 31, f = value & 31;
for (; a < 8; b = 8, a++)
{
for (; b < 32; c = 0, b++)
{
if (a != b)
{
for (; c < 32; d = 0, c++)
{
if (a != c && b != c)
{
for (; d < 32; e = 0, d++)
{
if (a != d && b != d && c != d)
{
for (; e < 32; f = 0, e++)
{
if (a != e && b != e && c != e && d != e)
{
for (; f < 32; f++)
{
if (a != f && b != f && c != f && d != f && e != f)
{
value = (a << 25) + (b << 20) + (c << 15) + (d << 10) + (e << 5) + f;
return value;
}
}
}
}
}
}
}
}
}
}
}
return null;
}
string keys = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
public string Base32encode() => keys.Substring((value >> 25) & 31,1) + keys.Substring((value >> 20) & 31,1) +
keys.Substring((value >> 15) & 31,1) + keys.Substring((value >> 10) & 31,1) +
keys.Substring((value >> 5) & 31,1) + keys.Substring(value & 31,1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment