将随机字节转换为指定范围内的整数
问题描述:
我正在尝试编写一个函数,该函数使用默认的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的同时手动执行此操作。
答
unsafe static int[] GenerateRandom(int length, int minInclusive, int maxExclusive)
{
var bytes = new byte[length * 4];
var ints = new int[length];
var ratio = uint.MaxValue/(double)(maxExclusive - minInclusive);
using (RandomNumberGenerator generator = RandomNumberGenerator.Create())
{
generator.GetBytes(bytes);
fixed(byte* b = bytes)
{
uint* i = (uint*)b;
for(int j = 0; j < length; j++, i++)
{
ints[j] = minInclusive + (int)(*i/ratio);
}
}
}
return ints;
}
我碰到一个小测试:
var ints = GenerateRandom(1000000, 0, 300);
var groups = ints.GroupBy(x => x).Select(g => new { value = g.Key, count = g.Count() });
var hist = Enumerable.Range(0, 300).Join(groups, x => x, g => g.value, (x, g) => new { value = x, count = g.count }).ToList();
var max = hist.OrderByDescending(x => x.value).First();
var min = hist.First();
而且效果十分随机所有数字0
和300
之间,min.count = 3301
和max.count = 3358
。
@MthetheWWatson:它是位于'System.Security.Cryptography'命名空间的抽象类。除非你想尝试一下,否则你可以忽略这个问题。 –
对不起,我发现刚刚发布。 –