sil*_*ver 3 c# random algorithm
有没有人知道为什么RNGCryptoServiceProvider在试图使数字大于300,000,000时失败进行卡方检验.
我试图得到0-1,000,000,000范围内的随机数,结果我得到了失败的卡方检验,0-300,000,000范围内的数字显得比其他数字更多.
最终我将大数字形式与较小的数字(0-99*100M + 0-99,999,999)和卡方测试通过相结合.
任何人都可以大量解释这个异常吗?
我使用以下代码来获取数字
[Timeout(TestTimeout.Infinite), TestMethod]
public void TestMethodStatistic()
{
Dictionary<long, long> appearances = new Dictionary<long, long>();
UInt64 tenBillion = 10000000000;
for (UInt64 i = 0; i < 10000000; i++)
{
UInt64 random = GetSIngleRandomNumberInternal() % tenBillion;
UInt64 bucket = random /10000000;
if (!appearances.ContainsKey(Convert.ToInt64(bucket)))
{
appearances.Add(Convert.ToInt64(bucket), 0);
}
appearances[Convert.ToInt64(bucket)]++;
}
string results = "\nBucket Id\tcount\n";
foreach (var appearance in appearances)
{
results += appearance.Key+"\t"+ appearance.Value +"\n";
}
File.AppendAllText(@"C:\Result.txt",results);
}
private RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
private UInt64 GetSIngleRandomNumberInternal()
{
byte[] randomNumBytes = new byte[sizeof(UInt64)];
rngCsp.GetBytes(randomNumBytes);
return BitConverter.ToUInt64(randomNumBytes, 0);
}
Run Code Online (Sandbox Code Playgroud)
获取Result.txt文件并将内容复制到excel.使它成为一个表并添加2列1是预期结果,值为100000,第二个是卡方检验,值为"= CHISQ.TEST([count],[[expected]])"
当卡方检验的值小于0.1时,我们就有问题了.