如何对伪随机数生成器进行单元测试? [英] How to unit test a pseudo random number generator?

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

问题描述

我有一个要进行单元测试的伪随机数生成器(PRNG)类.有两种方法:

I have a pseudo random number generator (PRNG) class that I want to unit test. There are two approaches:

  1. 编写一个包含大量样本的测试用例,并测试它们是否正确分发.这种方法可能导致测试用例的执行时间很长;
  2. 手工"计算一小部分样本,并验证PRNG算法是否重现了该样本.这种方法可能会导致生成不随机的序列而不会引起注意;

我要说的第一种方法并不是真正的单元测试,因为它不执行生成器的白盒测试,但是另一方面,它可以正确地测试类的责任.第二种方法更像是真实的单元测试,重点是算法,但是它不能提供有关类是否履行职责的足够证据.

I would say that the first approach is not really unit testing because it does not perform a white box test of the generator, but on the other hand it properly tests the responsibility of the class. The second approach is more like a real unit test, focusing on the algorithm, but it does not provide as much evidence as to whether the class fulfills its responsibility.

您偏爱哪种方法?为什么?

Which approach do you prefer, and why?

推荐答案

获取同一PRNG算法的另一种实现,基于已知种子生成少量的冗长的测试用例,并验证您对算法的实现是否与其他人的匹配实现.您测试的数据越多,机会就越大.如果您想当真话,请研究如何对算法进行FIPS验证.

Get another implementation of the same PRNG algorithm, generate a smallish number of longish test cases based on known seeds, and verify that your implementation of the algorithm matches everyone else's implementations. The more data you test, the more chance it does. If you want to be serious, look into how FIPS validation is done for the algorithm.

不需要测试输出是否是随机的,因为其他人对该算法的研究远远超出了您能够复制的能力.

There's no need to test whether the output is random, since far more research has been done on the algorithm by others than you are capable of reproducing.

如果您发明了自己的PRNG算法,那么您将面临一个完全不同的问题,因为除了测试您的代码外,您还需要测试您的新算法.有很多事情要做-我认为最重要的是对输出进行统计测试,并由其他密码学家进行同行审查.但是,基本上,如果您在设计PRNG算法时却没有足够的专业知识来测试它,那将是垃圾.

If you have invented your own PRNG algorithm then you have a rather different problem, because quite aside from testing your code you also need to test your new algorithm. There are various things to do -- I think the most important are statistical testing on the output, and peer review by other cryptographers. Basically, though, if you were to design a PRNG algorithm without having enough knowledge in the field to know how to test it, then it will be rubbish.

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

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