Skip to content

Instantly share code, notes, and snippets.

@zhenlinyang
Last active August 24, 2016 11:18
Show Gist options
  • Save zhenlinyang/514578fa4fd4065775ee5ae2967ebe1d to your computer and use it in GitHub Desktop.
Save zhenlinyang/514578fa4fd4065775ee5ae2967ebe1d to your computer and use it in GitHub Desktop.
Get No-repeat Random Value
public static class RandomMng
{
private static Random random = new Random();
public static int GetRandomInt32(ICollection<int> coll)
{
int ret;
do
{
ret = random.Next();
} while (coll.Contains(ret));
return ret;
}
public static ulong GetRandomUInt64(ICollection<ulong> coll)
{
ulong ret;
do
{
var num = new byte[8];
random.NextBytes(num);
ret = BitConverter.ToUInt64(num, 0);
} while (coll.Contains(ret));
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment