随机数发生器测试 [英] random number generator test

查看:103
本文介绍了随机数发生器测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何随机数生成器生成实际的随机数?



我的方法:首先构建大小为M的散列,其中M是素数。然后取数字
由随​​机数生成器生成,并采用mod $与$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
这是我的做法。我们可以用可视化来证明它吗?



由于我对测试的知识很少。你能建议我一个这个问题的彻底的方法吗?感谢提前

解决方案

您应该知道,您不能保证随机数生成器正常工作。请注意,即使是范围内的完美均匀分布[1,10] - 在10个数字的随机抽样中有10次获得10次10​​次的机会。



可能吗?



所以 - 我们可以做什么?



我们可以如果随机数生成器确实是均匀分布的,则统计证明组合(10,10,....,10)不太可能。这个概念称为 假设测试 。使用这种方法,我们可以说确定性x% - 我们可以拒绝数据从均匀分布中获取的假设。



一种常见的方法它正在使用 Pearson's Chi-Squared测试 ,这个想法与你的想法是一样的 - 你填写一张表格 - 检查每个单元格的号码数量(生成的)数字,以及什么是预期数字对于每个单元在零假设下(在您的情况下,预期为 k / M - 其中M是范围的大小,k是所用数字的总数)。

然后对数据进行一些操作(有关更多信息,请参阅维基百科文章),并获取一个数字(测试统计量)。然后,您可以检查这个数字是否可能
Chi-Square分布。如果是 - 您不能拒绝零假设,如果不是 - 您可以确定x%确定数据不是从均匀随机生成器中获取。



编辑:示例:

您有一个多维数据集,您要检查是否公平(均匀分布在 [1,6] )。投掷200次(例如)并创建下表:

 数字:1 2 3 4 5 6 
实证发生:37 41 30 27 32 33
预期发生:33.3 33.3 33.3 33.3 33.3 33.3

现在,根据皮尔森的测试,统计数据是:

  X =((37-33.3)^ 2)/33.3 + ((41-33.3)^ 2)/33.3 + ... +((33-33.3)^ 2)/33.3 
X =(18.49 + 59.29 + 10.89 + 39.69 + 1.69 + 0.09)/ 33.3
X = 3.9

对于随机的 C〜ChiSquare(5),高于 3.9 的概率为〜0.45 (这不是不可能的)< sup> 1 。



所以我们不能拒绝零假设,我们可以得出结论,数据可能是均匀分布在 [1,6]







How will you test if the random number generator is generating actual random numbers?

My Approach: Firstly build a hash of size M, where M is the prime number. Then take the number generated by random number generator, and take mod with M. and see it fills in all the hash or just in some part. That's my approach. Can we prove it with visualization?

Since I have very less knowledge about testing. Can you suggest me a thorough approach of this question? Thanks in advance

解决方案

You should be aware that you cannot guarantee the random number generator is working properly. Note that even a perfect uniform distribution in range [1,10] - there is a 10-10 chance of getting 10 times 10 in a random sampling of 10 numbers.

Is it likely? Of course not.

So - what can we do?

We can statistically prove that the combination (10,10,....,10) is unlikely if the random number generator is indeed uniformly distributed. This concept is called Hypothesis testing. With this approach we can say "with certainty level of x% - we can reject the hypothesis that the data is taken from a uniform distribution".

A common way to do it, is using Pearson's Chi-Squared test, The idea is similar to yours - you fill in a table - check what is the observed (generated) number of numbers for each cell, and what is the expected number of numbers for each cell under the null hypothesis (in your case, the expected is k/M - where M is the range's size, and k is the total number of numbers taken).
You then do some manipulation on the data (see the wikipedia article for more info what this manipulation is exactly) - and get a number (the test statistic). You then check if this number is likely to be taken from a Chi-Square Distribution. If it is - you cannot reject the null hypothesis, if it is not - you can be certain with x% certainty that the data is not taken from a uniform random generator.

EDIT: example:
You have a cube, and you want to check if it is "fair" (uniformly distributed in [1,6]). Throw it 200 times (for example) and create the following table:

number:                1       2         3         4          5          6
empirical occurances: 37       41        30        27         32         33
expected occurances: 33.3      33.3      33.3      33.3       33.3       33.3

Now, according to Pearson's test, the statistic is:

X = ((37-33.3)^2)/33.3 + ((41-33.3)^2)/33.3 + ... + ((33-33.3)^2)/33.3 
X = (18.49 + 59.29 + 10.89 + 39.69 + 1.69 + 0.09) / 33.3
X = 3.9

For a random C~ChiSquare(5), the probability of being higher then 3.9 is ~0.45 (which is not improbable)1.

So we cannot reject the null hypothesis, and we can conclude that the data is probably uniformly distributed in [1,6]


(1) We usually reject the null hypothesis if the value is smaller then 0.05, but this is very case dependent.

这篇关于随机数发生器测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆