public override long NextInt64(long minValue, long maxValue) { ulong exclusiveRange = (ulong)(maxValue - minValue); if (exclusiveRange <= int.MaxValue) { return(Next((int)exclusiveRange) + minValue); } if (exclusiveRange > 1) { // Narrow down to the smallest range [0, 2^bits] that contains maxValue. // Then repeatedly generate a value in that outer range until we get one within the inner range. int bits = BitOperations.Log2Ceiling(exclusiveRange); while (true) { ulong result = NextUInt64() >> (sizeof(ulong) * 8 - bits); if (result < exclusiveRange) { return((long)result + minValue); } } } Debug.Assert(minValue == maxValue || minValue + 1 == maxValue); return(minValue); }
public override long NextInt64(long maxValue) { if (maxValue <= int.MaxValue) { return(Next((int)maxValue)); } if (maxValue > 1) { // Narrow down to the smallest range [0, 2^bits] that contains maxValue. // Then repeatedly generate a value in that outer range until we get one within the inner range. int bits = BitOperations.Log2Ceiling((ulong)maxValue); while (true) { ulong result = NextUInt64() >> (sizeof(ulong) * 8 - bits); if (result < (ulong)maxValue) { return((long)result); } } } Debug.Assert(maxValue == 0 || maxValue == 1); return(0); }