# 蒙特卡洛算法

蒙特卡洛算法是一种随机化算法，它总是在固定时间界内结束，但可能以某个有界的错误概率返回不正确的结果。这个名称与拉斯维加斯算法相对，后者总是返回正确答案（或报告失败），但消耗随机量的时间或内存。在蒙特卡洛算法中，错误通常可以通过投入更多资源来减少，并且对于任何固定的计算预算，它都能给出一个近似答案。<sup>[1](https://www.deeplearningbook.org/contents/monte_carlo.html)</sup> 美国国家标准与技术研究院的《算法与数据结构词典》将其定义为一种可能产生不正确结果、但错误概率有界的随机化算法。<sup>[2](https://xlinux.nist.gov/dads/HTML/monteCarlo.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 运行时间确定、但可能以有界错误概率输出不正确结果的随机化算法<sup>[2](https://xlinux.nist.gov/dads/HTML/monteCarlo.html)</sup><sup> • </sup><sup>[3](https://stanford.edu/~lanhuong/refresher/notes/discrete_refresher_notes_2015.pdf)</sup> |
| 单边误差 | 若存在有效解，则以至少1/2的概率返回该解；若不存在，则总是报告失败<sup>[4](https://www.nationalacademies.org/read/2026/chapter/11)</sup> |
| 双边误差 | 以至少1/2 + ε的概率给出正确答案，其中ε > 0<sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup> |
| 错误缩减 | k次独立运行将1/2的成功概率提升至1 − 1/2<sup>k</sup>；对1/2 + ε算法进行t次运行并取多数票，正确概率至少为1 − e<sup>−2ε²t</sup><sup> • </sup><sup>[4](https://www.nationalacademies.org/read/2026/chapter/11)</sup><sup> • </sup><sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup> |
| 复杂度类 | 多项式时间单边误差蒙特卡洛算法定义了RP；多项式时间双边误差算法定义了BPP<sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup> |
| 名称起源 | 以蒙特卡洛赌场命名，拉斯维加斯算法亦然<sup>[6](https://web.cs.dal.ca/~nzeh/Teaching/4113/book/lp_rounding/randomized_rounding/randomization.html)</sup> |
| 奠基性文献 | Nicholas Metropolis与Stanislaw Ulam，《蒙特卡洛方法》，《美国统计协会杂志》44(247):335–341，1949年<sup>[7](https://doi.org/10.1080/01621459.1949.10483310)</sup> |

## 单边误差与双边误差

对于判定问题，蒙特卡洛算法根据哪些答案可能出错分为两个子类。在单边误差算法中，若真实答案为“是”，算法总是输出“是”；若真实答案为“否”，算法以至少p > 0的概率输出“否”；交换“是”与“否”的角色仍算作单边误差。<sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup> 等价地说，这类算法可以证伪一个断言，但永远无法确凿地证明它：重复运行可以增强对被检验命题的信心，但出错的可能性始终存在。<sup>[8](https://cs.stanford.edu/people/eroberts/courses/soco/projects/1998-99/randomized-algorithms/introduction/monte_carlo.html)</sup> 例子包括随机抽样、见证数量丰富性以及交互式证明。<sup>[8](https://cs.stanford.edu/people/eroberts/courses/soco/projects/1998-99/randomized-algorithms/introduction/monte_carlo.html)</sup>

在双边误差算法中，答案在每种输入上都可能出错，但算法以至少1/2 + ε的概率给出正确答案，其中ε > 0。指纹识别是双边蒙特卡洛算法的标准例子。<sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup><sup> • </sup><sup>[8](https://cs.stanford.edu/people/eroberts/courses/soco/projects/1998-99/randomized-algorithms/introduction/monte_carlo.html)</sup> 美国国家研究委员会1992年的报告《概率与算法》将“蒙特卡洛”一词专门用于单边情形，并指出其他类型的随机化算法也可以定义，但蒙特卡洛算法是最常用的那一类。<sup>[4](https://www.nationalacademies.org/read/2026/chapter/11)</sup>

## 缩减误差

每次运行保证很弱，这正是设计的关键所在：它可以被廉价地放大。给定一个单边误差算法，运行t次，只要有任何一次运行说“否”就输出“否”，则错误概率至多为(1 − p)<sup>t</sup>。<sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup> 美国国家科学院报告用自己的话表达了同样的思想：k次独立运行将1/2的成功概率提升至1 − 1/2<sup>k</sup>，因此只要运行足够多次，任何期望的置信水平都可以达到。<sup>[4](https://www.nationalacademies.org/read/2026/chapter/11)</sup>

对于双边误差算法，标准做法是重复运行t次并输出多数答案。若每次运行正确的概率为1/2 + ε，则多数答案正确的概率至少为1 − e<sup>−2ε²t</sup>，因此即使只有轻微的偏向正确的偏差也足够了。<sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup>

## 与拉斯维加斯算法的关系

这两个家族在时间与确定性之间进行权衡。拉斯维加斯算法总是返回精确的正确答案或报告失败，消耗随机量的资源；蒙特卡洛算法返回的答案带有随机量的误差，可以通过投入更多运行时间和内存来减少。<sup>[1](https://www.deeplearningbook.org/contents/monte_carlo.html)</sup> 每个拉斯维加斯算法都可以通过施加最大运行时间并在达到限制时输出当前最佳猜测，转化为蒙特卡洛算法；转化结果具有确定的运行时间和一定的错误概率。<sup>[3](https://stanford.edu/~lanhuong/refresher/notes/discrete_refresher_notes_2015.pdf)</sup> 这种转化在一般情况下能否反向进行，以及随机性对于任何高效算法是否必不可少（即P = BPP问题），在复杂度理论中仍未解决。<sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup>

## 复杂度类

RP类（随机多项式时间）由这样的语言组成：其成员资格可以由概率[图灵机](https://www.edgepedia.org/zh/turing-machine)在多项式时间内判定，且没有错误接受、错误拒绝少于一半——若输入不属于该语言，机器总是拒绝；若输入属于该语言，机器以至少1/2的概率接受。<sup>[9](https://xlinux.nist.gov/dads/HTML/rp.html)</sup> 这恰好是具有多项式时间单边误差蒙特卡洛算法的问题类。BPP类（有界误差概率多项式）则汇集了可由双边误差蒙特卡洛算法在多项式时间内求解的问题。<sup>[5](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)</sup>

去随机化，即确定性地模拟这些算法的纲领，数十年来一直推动着复杂度理论的发展；Lijie Chen与Roei Tell在2023年的一篇综述中描述了针对BPP = P猜想的新方法，包括伪随机生成器是否真的必要、以及哪些困难性假设足以支撑去随机化等问题。<sup>[10](https://eccc.weizmann.ac.il/report/2023/094/)</sup>

## 经典例子

**素性检验。** 时任加州大学伯克利分校计算机科学家的Gary L. Miller于1976年提出了两种检验整数素性的算法：第一种运行O(n<sup>1/7</sup>)步，改进了此前由Pollard给出的O(n<sup>1/4</sup>)最佳已知界；第二种运行O(log<sup>4</sup> n)步，但假设广义黎曼猜想成立，表明素性可以在二进制表示长度的多项式时间内检验。<sup>[11](https://www.sciencedirect.com/science/article/pii/S0022000076800438)</sup><sup> • </sup><sup>[12](https://doi.org/10.1145/800116.803773)</sup> 由这一系列工作发展而来的Miller–Rabin检验是一种单边误差蒙特卡洛检验：素数总能通过每一轮，而合数可能蒙混过关。美国国家标准与技术研究院的FIPS 186-4标准规定了密码学素数生成所需的Miller–Rabin检验轮数，例如在错误概率为2<sup>−80</sup>时，512位素数需要28轮。<sup>[13](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf)</sup>

**矩阵乘积验证。** 1977年，Freivalds提出了一种随机化算法，用于验证矩阵C′是否为两个n × n矩阵A与B的乘积，运行时间为O(n²)；它一直是展示随机化威力的最流行例子之一。尽管人们进行了大量努力，试图在不显著增加时间复杂度的前提下将其去随机化，但只取得了部分成功，例如将随机比特数降至对数级别。<sup>[14](https://link.springer.com/article/10.1007/s00453-016-0202-3)</sup> 该检验在每次迭代中以至少1/2的概率检测出每个错误行，独立迭代使错误概率指数下降。<sup>[14](https://link.springer.com/article/10.1007/s00453-016-0202-3)</sup>

## 数值计算中的蒙特卡洛方法

在数值计算中，该术语涵盖更广泛的一类抽样方法。写成期望的和或积分s = E<sub>p</sub>[f(x)]，可以通过从p中抽取n个样本并构成经验平均值来近似；该估计量是无偏的，其方差为Var[f(x)]/n，中心极限定理使得可以在估计值周围放置置信区间。<sup>[1](https://www.deeplearningbook.org/contents/monte_carlo.html)</sup> 当从p直接抽样不可行时，重要性抽样对从不同分布抽取的样本重新加权，[马尔可夫链蒙特卡洛](https://www.edgepedia.org/zh/markov-chain-monte-carlo)则构建一列收敛到目标分布的估计量。<sup>[1](https://www.deeplearningbook.org/contents/monte_carlo.html)</sup> 洛斯阿拉莫斯实验室的[Nicholas Metropolis](https://www.edgepedia.org/zh/nicholas-metropolis)与Stanislaw Ulam于1949年的奠基性论文将该方法作为数学物理中出现的微分方程与积分微分方程的统计方法提出。<sup>[7](https://doi.org/10.1080/01621459.1949.10483310)</sup>

蒙特卡洛算法的概念并不绑定于单一机器模型：它同样适用于顺序、并行或分布式环境下的算法。<sup>[4](https://www.nationalacademies.org/read/2026/chapter/11)</sup>

## References

1. [Goodfellow, Bengio, and Courville, Deep Learning, Chapter 17: Monte Carlo Methods](https://www.deeplearningbook.org/contents/monte_carlo.html)
2. [Monte Carlo algorithm, NIST Dictionary of Algorithms and Data Structures](https://xlinux.nist.gov/dads/HTML/monteCarlo.html)
3. [Discrete Math Refresher Notes, Stanford University (2015)](https://stanford.edu/~lanhuong/refresher/notes/discrete_refresher_notes_2015.pdf)
4. [National Research Council (1992), Probability and Algorithms, Chapter 10: Randomization in Parallel Algorithms](https://www.nationalacademies.org/read/2026/chapter/11)
5. [CS265/CME309 Randomized Algorithms, Lecture 1, Stanford University](https://theory.stanford.edu/~valiant/teaching/CS265/lectureNotes/l1.pdf)
6. [Randomized Algorithms: Prelude, Algorithms II course notes, Dalhousie University](https://web.cs.dal.ca/~nzeh/Teaching/4113/book/lp_rounding/randomized_rounding/randomization.html)
7. [Metropolis, N. and Ulam, S. (1949). The Monte Carlo Method. Journal of the American Statistical Association 44(247):335–341](https://doi.org/10.1080/01621459.1949.10483310)
8. [Monte Carlo Algorithms, Randomized Algorithms project, Stanford CS](https://cs.stanford.edu/people/eroberts/courses/soco/projects/1998-99/randomized-algorithms/introduction/monte_carlo.html)
9. [RP, NIST Dictionary of Algorithms and Data Structures](https://xlinux.nist.gov/dads/HTML/rp.html)
10. [Chen, L. and Tell, R. (2023). New ways of studying the BPP = P conjecture. ECCC TR23-094](https://eccc.weizmann.ac.il/report/2023/094/)
11. [Miller, G. L. (1976). Riemann's Hypothesis and Tests for Primality. Journal of Computer and System Sciences 13(3):300–317](https://www.sciencedirect.com/science/article/pii/S0022000076800438)
12. [Miller, G. L. (1975). Riemann's Hypothesis and Tests for Primality. STOC '75, pp. 234–239](https://doi.org/10.1145/800116.803773)
13. [NIST FIPS 186-4, Digital Signature Standard, Appendix C.3](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf)
14. [Efficiently Correcting Matrix Products. Algorithmica (Springer)](https://link.springer.com/article/10.1007/s00453-016-0202-3)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Computational complexity › Randomized, counting, and interactive classes

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/monte-carlo-algorithm

English original: https://www.edgepedia.org/monte-carlo-algorithm
