我需要一个方法,它返回从最小值到最大值的随机数,包括两个数字在内。我发现从斯蒂芬Toub和Shawn Farkas的,文章.NET Matters: Tales from the CryptoRandom在一些代码中的方法将是这个样子:RNGCryptoServiceProvider同时包含最小值和最大值
// Note - maxValue is excluded!
public static int GetRandomIntBetween(int minValue, int maxValue)
{
if (minValue > maxValue) throw new ArgumentOutOfRangeException("minValue");
if (minValue == maxValue) return minValue;
var rng = new RNGCryptoServiceProvider();
var uint32Buffer = new byte[4];
long diff = maxValue - minValue;
while (true)
{
rng.GetBytes(uint32Buffer);
uint rand = BitConverter.ToUInt32(uint32Buffer, 0);
const long max = (1 + (long)int.MaxValue);
long remainder = max % diff;
if (rand < max - remainder)
{
return (int)(minValue + (rand % diff));
}
}
}
我试图使包括maxValue包括:
public static int GetRandomIntBetween(int minValue, int maxValue)
{
if (minValue > maxValue) throw new ArgumentOutOfRangeException("minValue");
if (minValue == maxValue) return minValue;
// Make maxValue inclusive.
maxValue++;
var rng = new RNGCryptoServiceProvider();
var uint32Buffer = new byte[4];
long diff = maxValue - minValue;
while (true)
{
rng.GetBytes(uint32Buffer);
uint rand = BitConverter.ToUInt32(uint32Buffer, 0);
const long max = (1 + (long)int.MaxValue);
long remainder = max % diff;
if (rand < max - remainder)
{
return (int)(minValue + (rand % diff));
}
}
}
看起来很奇怪,但似乎我可以保留前两个检查,即使语义略有不同,它仍然可以工作。结果数据看起来也不错。我错过了什么,或者我的更改是否正常?
PS - 我在问这个问题,因为生成随机数显然是一个非常微妙的问题,并且希望确定我的方法是正确的。