# 随机性检验

随机性检验是一种统计程序，用于判断一个有序数据序列是否与随机过程产生的数据相一致，检测趋势、振荡和自相关（数据序列与其自身过去值的相关性）等模式。每一种此类检验都形式化一个原假设：该序列是以随机方式产生的；检验统计量超出临界值则拒绝该假设。<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup><sup> • </sup><sup>[2](https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-22r1a.pdf)</sup> 这些检验服务于两大类受众。在统计学和时间序列分析中，它们检验观测数据是否存在序列依赖性；在密码学中，它们作为测试组套用于伪随机数和硬件随机数生成器，NIST明确指出，没有任何一组统计检验能够绝对证明一个生成器的随机性，统计检验也不能替代密码分析。<sup>[2](https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-22r1a.pdf)</sup> 最常见的具名检验是沃尔德-沃尔福威茨游程检验，也称为单样本游程检验，以及升降游程检验。<sup>[3](https://www.oreilly.com/library/view/statistical-hypothesis-testing/9781118762615/9781118762615c13.xhtml)</sup>

| 属性 | 详情 |
|---|---|
| 原假设 | 序列以随机方式产生<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup> |
| 主要统计量 | \( Z = \frac{R - \bar{R}}{s_{R}} \)，其中 \( R \) 为观测到的游程数<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup> |
| 原假设下的均值与方差 | \( \mu_{R} = \frac{2n_{1} \cdot n_{2}}{n_{1}+n_{2}} + 1 \)；\( \mathrm{Var}(R) = \frac{2n_{1} \cdot n_{2}(2n_{1} \cdot n_{2} - n_{1} - n_{2})}{(n_{1}+n_{2})^{2}(n_{1}+n_{2}-1)} \)<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup><sup> • </sup><sup>[4](https://online.stat.psu.edu/stat415/book/export/html/925)</sup> |
| 原假设下的精确分布 | \( R \) 的二项式系数概率函数，可用于小样本中的精确p值<sup>[5](https://rdrr.io/cran/randtests/man/runs.html)</sup> |
| 正态近似 | 大样本条件为 \( n_{1} > 10,\ n_{2} > 10 \)（NIST手册）或 \( n_{1} \geq 10,\ n_{2} \geq 10 \)（宾夕法尼亚州立大学讲义）<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup><sup> • </sup><sup>[4](https://online.stat.psu.edu/stat415/book/export/html/925)</sup> |
| 测试组套标准 | NIST SP 800-22 Rev. 1：15项检验；指南建议至少 \( 10^{8} \) 比特的数据<sup>[2](https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-22r1a.pdf)</sup><sup> • </sup><sup>[6](https://www.mdpi.com/1099-4300/26/12/1053)</sup> |

## 工作原理

在原假设下，数据的排序是可交换的。对于大小分别为 \( n_{1} \) 和 \( n_{2} \) 的两个样本，将其合并并按数值大小排序后，两种标签的每一种排列都是等可能的，由此得到游程数 \( R \) 的精确概率函数。<sup>[4](https://online.stat.psu.edu/stat415/book/export/html/925)</sup> 游程是由相同标签的值连续构成、且不能再延长的序列。在单样本情形中，高于参考值（通常为中位数）的值编码为正，低于参考值的编码为负。<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup> 对于偶数 \( r \)，精确概率为

\[ P(R=r) = \frac{2\binom{n_{1}-1}{r/2-1}\binom{n_{2}-1}{r/2-1}}{\binom{n_{1}+n_{2}}{n_{1}}} \]

对于奇数 \( r \)，则有两项二项式系数表达式。<sup>[5](https://rdrr.io/cran/randtests/man/runs.html)</sup> 对于大样本，精确分布由正态近似替代，均值和方差见表中所示，因此将 \( Z = \frac{R - \mu_{R}}{\sqrt{\mathrm{Var}(R)}} \) 与标准正态表进行比较；在5%显著性水平下，绝对值超过1.96表明非随机性。<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup><sup> • </sup><sup>[4](https://online.stat.psu.edu/stat415/book/export/html/925)</sup>

偏离的方向具有诊断意义。游程过少表明存在趋势；游程过多表明存在周期性或振荡效应。<sup>[7](https://online.stat.psu.edu/stat415/lesson/21/21.2)</sup>

## 操作步骤

**第一步，二分。** 对于数值数据，参考值可以是均值、中位数、众数或自定义值；高于参考值的编码为1，低于的编码为0，二值或分类数据可直接使用。<sup>[8](https://www.ncss.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Analysis_of_Runs.pdf)</sup> 在中位数处的处理惯例有所不同：NIST手册省略等于中位数的值，<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup> 而一种常见的替代方案将奇数样本量的额外观测值以及并列的中位数值归入上半部分。<sup>[7](https://online.stat.psu.edu/stat415/lesson/21/21.2)</sup>

**第二步，计数编码序列中的游程** \( R \)。**第三步，计算p值。** NCSS等软件同时计算精确检验和渐近检验，并指出精确检验比两种渐近z检验更准确，若有条件应始终使用。<sup>[8](https://www.ncss.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Analysis_of_Runs.pdf)</sup> **第四步，在渐近检验中进行连续性校正**：SAS在观测数低于50时施加±0.5的校正，校正后的统计量为 \( z = \frac{r - \mu_{r} - \mathrm{sgn}(r-\mu_{r})/2}{\sigma_{r}} \)。<sup>[9](https://support.sas.com/kb/33/092.html)</sup><sup> • </sup><sup>[10](https://bookdown.org/blazej_kochanski/statistics2/nonparametric.html)</sup>

实例展示了两种结果。NIST手册中200次梁偏转测量得到 \( Z = 2.6938 \)，超过1.96的临界值，因此在0.05水平下判定数据为非随机，<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup> 而SAS对75个随机数生成器值的示例给出 \( p = 0.9988 \)，未能拒绝随机性。<sup>[9](https://support.sas.com/kb/33/092.html)</sup>

## 起源

游程检验得名于A·沃尔德和J·沃尔福威茨1940年发表于《数理统计年鉴》的论文《关于两样本是否来自同一总体的检验》，该文分析了两样本合并排序中的游程。<sup>[11](https://doi.org/10.1214/aoms/1177731909)</sup> 1943年，弗里达·S·斯韦德和C·艾森哈特发表了用于检验由两种取值构成的序列中分组随机性的精确表，为小样本提供了临界值。<sup>[12](https://doi.org/10.1214/aoms/1177731494)</sup> 升降变体通过列出连续项之间差值的符号并形成这些符号中游程长度的频率分布来考察相位持续时间。<sup>[13](http://webspace.ship.edu/pgmarr/Geo441/Readings/Wallis%20and%20Moore%201946%20-%20A%20Significance%20Test%20for%20Time%20Series%20Analysis.pdf)</sup> 罗伯特·巴特尔斯于1982年在《美国统计协会杂志》上发表了冯·诺依曼比率检验的秩版本用于随机性检验，在随机化假设下获得了临界值。<sup>[14](https://doi.org/10.1080/01621459.1982.10477764)</sup> 在生成器测试方面，NIST统计测试套件于2000年由安德鲁·鲁欣及其同事记录成文，<sup>[15](https://doi.org/10.6028/nist.sp.800-22)</sup> 乌利·M·毛雷尔于1992年在《密码学杂志》上发表了针对随机比特生成器的通用统计检验。<sup>[16](https://doi.org/10.1007/bf00193563)</sup>

## 变体

**基于游程的检验。** 单样本游程检验将一个序列围绕参考值进行二分；两样本沃尔德-沃尔福威茨检验合并两个样本并检验它们是否来自同一总体，游程数较少时拒绝。<sup>[3](https://www.oreilly.com/library/view/statistical-hypothesis-testing/9781118762615/9781118762615c13.xhtml)</sup><sup> • </sup><sup>[4](https://online.stat.psu.edu/stat415/book/export/html/925)</sup> 升降游程检验计数连续上升和下降的游程；对于 \( n \) 个数值，期望游程数为 \( (2n-1)/3 \)，NCSS仅对 \( n \leq 25 \) 计算其精确检验。<sup>[8](https://www.ncss.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Analysis_of_Runs.pdf)</sup> 冯·诺依曼比率检验考察连续差值；巴特尔斯的秩版本将统计量

\[ R_{VN} = \frac{\sum_{i=1}^{n-1}(R_{i} - R_{i+1})^{2}}{\sum_{i=1}^{n}\left(R_{i} - \frac{n+1}{2}\right)^{2}} \]

应用于秩。<sup>[14](https://doi.org/10.1080/01621459.1982.10477764)</sup><sup> • </sup><sup>[17](https://cran.r-project.org/web/packages/randtests/refman/randtests.html)</sup> R包randtests将其与游程检验、考克斯-斯图尔特检验、差值符号检验、曼-肯德尔秩检验和转折点检验一同实现。<sup>[17](https://cran.r-project.org/web/packages/randtests/refman/randtests.html)</sup> 基于高阶符号自相关的广义游程检验将检测扩展到 \( h \) 阶序列依赖性。<sup>[18](https://dipot.ulb.ac.be/dspace/bitstream/2013/138031/1/vnn094.pdf)</sup>

**测试组套。** NIST SP 800-22 Rev. 1套件由15项检验组成：频率（单比特）、块内频率、游程、最长连续1、二进制矩阵秩、DFT（谱）、非重叠和重叠模板匹配、毛雷尔通用、线性复杂度、序列、近似熵、累积和、随机游走和随机游走变体。<sup>[2](https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-22r1a.pdf)</sup> 谱检验检查离散快速傅里叶变换中的峰值高度以检测周期性特征，套件中的游程检验则考察1和0游程之间的振荡是否过快或过慢。<sup>[19](https://csrc.nist.gov/Projects/Random-Bit-Generation/Documentation-and-Software/Guide-to-the-Statistical-Tests)</sup> 在NIST之外，Diehard套件、Dieharder和TestU01是其他广泛使用的测试组套。<sup>[20](https://dl.acm.org/doi/10.1145/3447773)</sup>

## 应用

在密码学实践中，SP 800-22已成为生成器统计评估的事实基准，但NIST在2022年决定修订该套件，其中包括澄清统计测试套件的目的和用途，不再将其用于评估密码学随机数生成器。<sup>[6](https://www.mdpi.com/1099-4300/26/12/1053)</sup><sup> • </sup><sup>[21](https://csrc.nist.gov/news/2022/decision-to-revise-nist-sp-800-22-rev-1a)</sup> 套件指南建议使用100个各含 \( 10^{6} \) 比特的子串、至少 \( 10^{8} \) 比特（12.5 MB）的数据，并在1%显著性水平下进行评估。<sup>[6](https://www.mdpi.com/1099-4300/26/12/1053)</sup>

在质量控制中，高于和低于中位数的观测值分别标记为U和L；游程过少表明过程存在趋势，游程过多则表明存在周期效应。<sup>[7](https://online.stat.psu.edu/stat415/lesson/21/21.2)</sup>

## 局限性与替代方案

**功效。** 游程检验主要对一阶结构敏感：由于它建立在一阶符号自相关量之上，可能无法检测高阶依赖性。<sup>[18](https://dipot.ulb.ac.be/dspace/bitstream/2013/138031/1/vnn094.pdf)</sup> 巴特尔斯的蒙特卡洛实验表明，秩冯·诺依曼比率检验的功效远大于基于升降游程数的检验。<sup>[14](https://doi.org/10.1080/01621459.1982.10477764)</sup> 非参数检验的功效通常低于其参数对应物。<sup>[10](https://bookdown.org/blazej_kochanski/statistics2/nonparametric.html)</sup>

**失效模式。** 对于两样本检验，组间的并列值会产生多种可能的序列；一种经典建议是对所有排列的P值取平均，但DescTools文档建议在并列值超过一两处时不要使用该检验。<sup>[22](https://andrisignorell.github.io/DescTools/reference/RunsTest.html)</sup> 小样本需要精确表或精确计算而非正态近似。<sup>[8](https://www.ncss.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Analysis_of_Runs.pdf)</sup><sup> • </sup><sup>[12](https://doi.org/10.1214/aoms/1177731494)</sup> “大样本”的阈值在各参考文献中有所不同：NIST手册要求 \( n_{1} > 10 \) 且 \( n_{2} > 10 \)，<sup>[1](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)</sup> 宾夕法尼亚州立大学的讲义使用 \( n_{1} \geq 10 \) 且 \( n_{2} \geq 10 \)，<sup>[4](https://online.stat.psu.edu/stat415/book/export/html/925)</sup> 而一份讲义来源假设 \( n \geq 30 \)。<sup>[10](https://bookdown.org/blazej_kochanski/statistics2/nonparametric.html)</sup> 在测试组套中，各项检验并非独立：近期工作推导了推广九项NIST检验的统计量的极限联合分布，以处理它们之间的依赖性。<sup>[23](https://geodesic.mathdoc.fr/item/DM_2024_36_2_a5/)</sup>

**展望。** NIST在2022年4月的规划说明中宣布，将作为其加密出版物审查项目的一部分修订SP 800-22。<sup>[24](https://csrc.nist.gov/pubs/sp/800/22/r1/upd1/final)</sup>

## References

1. [1.3.5.13. Runs Test for Detecting Non-randomness (NIST/SEMATECH e-Handbook)](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35d.htm)
2. [NIST SP 800-22r1a full text](https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-22r1a.pdf)
3. [Statistical Hypothesis Testing with SAS and R (Taeger & Kuhnt), Chapter 13: Tests on randomness](https://www.oreilly.com/library/view/statistical-hypothesis-testing/9781118762615/9781118762615c13.xhtml)
4. [STAT 415, Lesson 21.1: The Run Test (Penn State)](https://online.stat.psu.edu/stat415/book/export/html/925)
5. [randtests::runs, Distribution of the Wald–Wolfowitz Runs Statistic](https://rdrr.io/cran/randtests/man/runs.html)
6. [Statistical Testing of Random Number Generators and Their Improvement Using Randomness Extraction (Entropy, MDPI)](https://www.mdpi.com/1099-4300/26/12/1053)
7. [STAT 415, Lesson 21.2: Test for Randomness (Penn State)](https://online.stat.psu.edu/stat415/lesson/21/21.2)
8. [NCSS Statistical Software, Analysis of Runs procedure documentation](https://www.ncss.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Analysis_of_Runs.pdf)
9. [SAS Sample 33092: Wald-Wolfowitz (or Runs) test for randomness](https://support.sas.com/kb/33/092.html)
10. [Statistics 2 lecture notes, Chapter 20: Nonparametric tests](https://bookdown.org/blazej_kochanski/statistics2/nonparametric.html)
11. [A. Wald, J. Wolfowitz (1940). On a Test Whether Two Samples are from the Same Population. The Annals of Mathematical Statistics.](https://doi.org/10.1214/aoms/1177731909)
12. [Frieda S. Swed, C. Eisenhart (1943). Tables for Testing Randomness of Grouping in a Sequence of Alternatives. The Annals of Mathematical Statistics.](https://doi.org/10.1214/aoms/1177731494)
13. [Wallis and Moore (1946), A Significance Test for Time Series Analysis](http://webspace.ship.edu/pgmarr/Geo441/Readings/Wallis%20and%20Moore%201946%20-%20A%20Significance%20Test%20for%20Time%20Series%20Analysis.pdf)
14. [Robert Bartels (1982). The Rank Version of von Neumann's Ratio Test for Randomness. Journal of the American Statistical Association.](https://doi.org/10.1080/01621459.1982.10477764)
15. [Andrew Rukhin and colleagues (2000). A statistical test suite for random and pseudorandom number generators for cryptographic applications. .](https://doi.org/10.6028/nist.sp.800-22)
16. [Ueli M. Maurer (1992). A universal statistical test for random bit generators. Journal of Cryptology.](https://doi.org/10.1007/bf00193563)
17. [randtests: Testing Randomness in R (version 1.0.2)](https://cran.r-project.org/web/packages/randtests/refman/randtests.html)
18. [Runs tests (Paindaveine, ULB)](https://dipot.ulb.ac.be/dspace/bitstream/2013/138031/1/vnn094.pdf)
19. [Guide to the Statistical Tests (NIST Random Bit Generation project)](https://csrc.nist.gov/Projects/Random-Bit-Generation/Documentation-and-Software/Guide-to-the-Statistical-Tests)
20. [Recommendations on Statistical Randomness Test Batteries for Cryptographic Purposes (ACM Computing Surveys)](https://dl.acm.org/doi/10.1145/3447773)
21. [Decision to Revise NIST SP 800-22 Rev. 1a | CSRC](https://csrc.nist.gov/news/2022/decision-to-revise-nist-sp-800-22-rev-1a)
22. [DescTools::RunsTest documentation (R)](https://andrisignorell.github.io/DescTools/reference/RunsTest.html)
23. [The limit joint distributions of statistics of tests of the NIST package and their generalizations (Discrete Mathematics and Applications, 2024)](https://geodesic.mathdoc.fr/item/DM_2024_36_2_a5/)
24. [SP 800-22 Rev. 1, A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications](https://csrc.nist.gov/pubs/sp/800/22/r1/upd1/final)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论 › Statistical inference, estimation, sampling, and testing › Hypothesis testing

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/randomness-test

English original: https://www.edgepedia.org/randomness-test
