You can write extension methods for Random as follows:
public static class RandomExt
{
public static long NextLong(this Random self, long min, long max)
{
// Get a random 64 bit number.
var buf = new byte[sizeof(ulong)];
self.NextBytes(buf);
ulong n = BitConverter.ToUInt64(buf, 0);
// Scale to between 0 inclusive and 1 exclusive; i.e. [0,1).
double normalised = n / (ulong.MaxValue + 1.0);
// Determine result by scaling range and adding minimum.
double range = (double)max - min;
return (long)(normalised * range) + min;
}
public static ulong NextULong(this Random self, ulong min, ulong max)
{
// Get a random 64 bit number.
var buf = new byte[sizeof(ulong)];
self.NextBytes(buf);
ulong n = BitConverter.ToUInt64(buf, 0);
// Scale to between 0 inclusive and 1 exclusive; i.e. [0,1).
double normalised = n / (ulong.MaxValue + 1.0);
// Determine result by scaling range and adding minimum.
double range = (double)max - min;
return (ulong)(normalised * range) + min;
}
}
(The algorithm I used to create a random double in the range [0,1) is the same as that used by the Random
class, except I converted it to use ulong
rather than ushort
.)
Then you can do this:
var rng = new Random();
ulong randomNumber = rng.NextULong(1000000000000000, 9999999999999999999);
Ideally you'd use a better random number generator (such as an XOR-SHIFT) with a longer period, but this will give you random numbers in the specified range.