0
我正在尝试编写一个函数,该函数使用默认的RandomNumberGenerator实现在指定范围内生成Int32值。将随机字节转换为指定范围内的整数
void GenerateRandom (int [] data, int minInclusive, int maxExclusive)
{
int size = 0;
int length = 0;
byte [] bytes = null;
size = (int) Math.Ceiling(Math.Log(Math.Abs(maxExclusive - minInclusive), 2));
length = data.Length * size;
var bytes = new byte [length];
using (RandomNumberGenerator generator = RandomNumberGenerator.Create())
{
generator.GetBytes(bytes);
}
// How to effectively convert this `byte []` to an `int []` within the specified range?
}
的一种尝试是产生长度(data.Length * ((int) Math.Ceiling(Math.Log(Math.Abs(maxExclusive - minInclusive), 2))))
的随机字节数组和字节的每x个结合到一个int。不管指定的范围如何,这种方法当然有一个巨大的偏向于较大值的缺点,因为多个最重要的字节几乎不可能是零。
任何输入,将不胜感激。虽然我在这里使用.NET,但平台/语言并不重要。寻找一个概念提示。
请注意,我已经熟悉.NET中的Random类,但我只想知道如何在使用RandomNumberGenerator的同时手动执行此操作。
@MthetheWWatson:它是位于'System.Security.Cryptography'命名空间的抽象类。除非你想尝试一下,否则你可以忽略这个问题。 –
对不起,我发现刚刚发布。 –