AceInfinity
Emeritus, Contributor
There are other methods other than using the Random class to generate random values. Not to mention it only supports int, and double for numbers.
Here's a fast function that generates nice random numbers from ntdll.dll:
There is a non-"ex" version of this, but this extended function is much better, and is the one you will want to be using if you choose to call to this function.
Here are my overloaded methods as a wrapper for returning usable random unsigned long values:
Here are some return values just from a bit of fun and play:
:beerchug2:
Here's a fast function that generates nice random numbers from ntdll.dll:
Code:
sealed internal class NativeMethods
{
[DllImport("ntdll.dll")]
internal static extern ulong RtlRandomEx(ref ulong Seed);
}
There is a non-"ex" version of this, but this extended function is much better, and is the one you will want to be using if you choose to call to this function.
Here are my overloaded methods as a wrapper for returning usable random unsigned long values:
Code:
public static ulong RandGen()
{
return RandGen(1);
}
public static ulong RandGen(ulong seed)
{
return RandGen(seed, 0, ulong.MaxValue);
}
public static ulong RandGen(ulong seed, ulong lowerBound, ulong upperBound)
{
if (lowerBound >= upperBound)
{
throw new ArgumentException("The lowerbound argument cannot be equal to or greater than the exclusive upperbound.");
}
ulong r = NativeMethods.RtlRandomEx(ref seed);
return (r %= upperBound - lowerBound) + lowerBound;
}
Here are some return values just from a bit of fun and play:
Code:
1586179657
510558172
1605617193
1424793008
1152951110
528928978
107522126
724979721
2106295624
458868389
Code:
for (int i = 0; i < 10; i++)
{
ulong x = (ulong)(int.MaxValue ^ i << i);
Console.WriteLine(RandGen(x, 0, ulong.MaxValue));
}
:beerchug2: