# 随机排列

随机排列是一组 n 个条目的随机排序，其中 n! 种可能排序中的每一种都等可能。当 n! 种可能排序中的每一种都以恰好 1/n! 的概率被选中时，n 个带标签条目的洗牌就是均匀的，前提是索引选择独立且均匀；对于含有重复条目的多重集，不同的排列列表少于 n! 种，而关于带标签排列的均匀性并不会给每个不同的列表分配 1/n! 的概率。<sup>[1](https://mathworld.wolfram.com/Fisher-YatesShuffle.html)</sup><sup> • </sup><sup>[2](https://isa-afp.org/browser_info/current/AFP/Fisher_Yates/document.pdf)</sup> 典型用途包括在操作系统和排序模拟中获取典型情况。<sup>[3](https://www.princeton.edu/~rblee/ELE572Papers/p137-sedgewick.pdf)</sup>

| 关键事实 | 数值 |
|---|---|
| 均匀性判据 | n! 种排列中的每一种都以 1/n! 的概率产生<sup>[1](https://mathworld.wolfram.com/Fisher-YatesShuffle.html)</sup> |
| 一次洗牌的熵 | \( \log_2(n!) \) 比特；一百万个条目需超过 1800 万比特<sup>[4](https://queue.acm.org/detail.cfm?id=3664645)</sup> |
| 标准算法 | Fisher–Yates（克努特）洗牌法：O(n) 时间，O(1) 辅助空间<sup>[1](https://mathworld.wolfram.com/Fisher-YatesShuffle.html)</sup> |
| 现代计算机版本 | 理查德·杜斯滕菲尔德，《算法 235：随机排列》，《ACM 通讯》，1964 年<sup>[5](https://doi.org/10.1145/364520.364540)</sup> |
| 常见错误 | 将每个元素与整个数组中的某个元素交换，得到 n^n 条路径对应 n! 种结果，这是有偏的<sup>[6](https://people.cs.umass.edu/~phaas/CS590M/handouts/Fisher-Yates-proof.pdf)</sup> |
| 流式数据 | 蓄水池算法在 O(n) 总时间内保持一个大小为 s 的均匀随机样本<sup>[7](http://www.cse.cuhk.edu.hk/~taoyf/course/wst5017/notes/lec9.pdf)</sup> |
| 已报道的最大规模 | 2025 年在最多 256 块 NVIDIA A100 GPU 上，约 1.1 秒内处理 1370 亿个值<sup>[8](https://doi.org/10.1145/3731599.3767436)</sup> |

## 原理

均匀性是关于对称群 \( S_n \) 的陈述：算法的输出分布必须给每个排列分配 1/n! 的概率。信息论代价随之而来，因为要在 n! 个等可能结果中加以区分需要 \( \log_2(n!) \) 个随机比特；因此洗牌一百万个条目消耗超过 1800 万比特的熵。<sup>[4](https://queue.acm.org/detail.cfm?id=3664645)</sup>

随机源本身限制了可达范围。一个由 32 位状态播种的伪随机数生成器能产生少于 13! 种不同结果，即便 128 位种子也只能覆盖一副纸牌 52! 种排列中不到 \( 1/2^{97} \) 的部分。<sup>[4](https://queue.acm.org/detail.cfm?id=3664645)</sup> 梅森旋转算法能生成的 2084 个条目的排列不到 1%。<sup>[9](https://ar5iv.labs.arxiv.org/html/1810.10985)</sup>

## 做法

Fisher–Yates 洗牌法以杜斯滕菲尔德的形式，一趟处理数组 S：处理第 i 个元素时，在 [1, i] 中生成一个随机索引 x，并交换 S[i] 与 S[x]；范围随 i 增大，因此每个元素只与其之前或自身位置的元素交换。<sup>[7](http://www.cse.cuhk.edu.hk/~taoyf/course/wst5017/notes/lec9.pdf)</sup> 使用随机访问数组和常数时间索引生成，该算法以 O(n) 时间和 O(1) 辅助空间运行。<sup>[1](https://mathworld.wolfram.com/Fisher-YatesShuffle.html)</sup>

均匀性证明将条件概率链式相乘：第一个位置正确的概率为 1/N，下一个为 1/(N−1)，依此类推直至 1/i，因此对每个排列乘积恰好为 1/N!。<sup>[6](https://people.cs.umass.edu/~phaas/CS590M/handouts/Fisher-Yates-proof.pdf)</sup> 算法的可能输出与 \( S_n \) 以及与乘积 [n] × [n−1] × ... × [1] 双射对应，在 \( S_n \) 上诱导的测度是均匀的。<sup>[10](https://www.cs.auckland.ac.nz/research/groups/CDMTCS/researchreports/295mcw.pdf)</sup> 洗牌的正确性，包括其正向和反向变体的等价性，也已在 Isabelle/HOL 中机器验证。<sup>[2](https://isa-afp.org/browser_info/current/AFP/Fisher_Yates/document.pdf)</sup>

## 起源

现代原地计算机版本由圣迭戈通用原子公司的理查德·杜斯滕菲尔德发表，题为《算法 235：随机排列》，载于《ACM 通讯》第 7 卷第 7 期，1964 年 7 月。<sup>[5](https://doi.org/10.1145/364520.364540)</sup> 杜斯滕菲尔德的论文提供了计算机实现；反向循环形式常被称为克努特洗牌法。<sup>[10](https://www.cs.auckland.ac.nz/research/groups/CDMTCS/researchreports/295mcw.pdf)</sup><sup> • </sup><sup>[11](https://www.jjj.de/pub/arndt-rand-perm-thesis.pdf)</sup><sup> • </sup><sup>[11](https://www.jjj.de/pub/arndt-rand-perm-thesis.pdf)</sup>

## 变体

**萨托洛算法**与 Fisher–Yates 完全相同，只是不允许自交换索引，因此 j 从 [n − i] 中均匀选取；其输出与 n 轮换双射对应，且在循环排列上均匀。两种算法均恰好执行 n − 1 次交换，但 Fisher–Yates 中某些交换可能是平凡的，而萨托洛算法中每次交换都是非平凡的。<sup>[10](https://www.cs.auckland.ac.nz/research/groups/CDMTCS/researchreports/295mcw.pdf)</sup>

**按随机键排序**为每个条目分配一个随机键并排序。称为 PIKK 的方法被批评为劣于 Fisher–Yates，后者需要 n 个随机整数且无需排序。<sup>[9](https://ar5iv.labs.arxiv.org/html/1810.10985)</sup> PyTorch 的 CUDA randperm 使用此策略并加以修正：它选择键宽以将重复键概率控制在阈值以下，然后用串行 Fisher–Yates 内核重新洗牌任何重复键值的“孤岛”。<sup>[12](https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/cuda/Randperm.cu)</sup>

**分治变体。** 拉奥-桑德利乌斯算法（算法 RS）通过抛硬币将条目分组，并对每组递归。<sup>[13](https://algo.stat.sinica.edu.tw/hk/files/2016/11/rand-perm-2016-v2.pdf)</sup>

**随机对换。** 通过 k 次独立均匀对换生成排列，当 n > 2 时永远不会精确均匀，迪亚科尼斯和沙赫沙哈尼的结果表明 k 必须超过 (1/2)·n·log n 才能在变差距离上接近均匀。<sup>[14](https://doi.org/10.1007/bf00535487)</sup>

**流与并行机器。** 蓄水池算法从无界流中维护一个大小为 s 的样本 R：对于第 n 个元素（n ≥ s + 1），从 1 到 n 中抽取 x，仅当 x ≤ s 时替换 R[x]，因此任何大小为 s 的子集都是等可能的，总时间为 O(n)。<sup>[7](http://www.cse.cuhk.edu.hk/~taoyf/course/wst5017/notes/lec9.pdf)</sup> 这是维特《蓄水池随机抽样》（《ACM 数学软件汇刊》，1985 年）的研究背景。<sup>[15](https://doi.org/10.1145/3147.3165)</sup> 对于并行执行，安德森证明了在公平原子交换下 Fisher–Yates 的交换可以重排而不引入偏差，Shun 等人证明了依赖图以高概率具有 O(log n) 的深度。<sup>[16](https://doi.org/10.1145/3505287)</sup> 在 GPU 上，米切尔等人的双射洗牌法（《ACM 并行计算汇刊》，2022 年）使用伪随机双射函数如 VariablePhilox 与并行压缩融合，是确定性且无碰撞的，并且每个元素需要一次全局内存读写，尽管它不是原地算法。<sup>[16](https://doi.org/10.1145/3505287)</sup>

## 应用

置换检验直接复用该原语：NIST Dataplot 的 SAMPLE RANDOM PERMUTATION 命令支持对均值差、中位数差或截尾均值差进行双样本置换检验，希金斯建议 1600 次随机排列足以满足大多数用途。<sup>[17](https://itl.nist.gov/div898/software/dataplot/refman2/auxillar/samprand.htm)</sup> 其他用途包括置换蒙特卡洛检验、自助采样以及特征归因的沙普利值近似。<sup>[16](https://doi.org/10.1145/3505287)</sup> 在密码学中，经典 Fisher–Yates 出现在 LESS 数字签名方案的密钥生成例程中。<sup>[18](https://eprint.iacr.org/2024/008.pdf)</sup> 在机器学习数据加载中，PyTorch 的洗牌使用纯蓄水池采样，CorgiPile（2024 年）用两级“先块后元组”策略替代完全洗牌，达到与完全洗牌一致的收敛效果。<sup>[19](https://link.springer.com/article/10.1007/s00778-024-00845-0)</sup>

## 局限与替代方案

**朴素交换是有偏的。** 每一步将 x[i] 与从完整范围 {1, ..., n} 中随机抽取的 J 交换，不会产生均匀排列。<sup>[6](https://people.cs.umass.edu/~phaas/CS590M/handouts/Fisher-Yates-proof.pdf)</sup> n^n 条等可能的执行路径映射到 n! 种排列，由于对任何大于 2 的 n，n! 不能整除 n^n，因此某些排列比其他排列出现得更频繁。<sup>[4](https://queue.acm.org/detail.cfm?id=3664645)</sup>

**取模与种子偏差。** 将随机整数对一个模数取模会引入偏差，除非各剩余类等可能；使用 rand() % N 时，判断 rand() % N < N/2 为真的频率可能为假的频率的两倍。<sup>[1](https://mathworld.wolfram.com/Fisher-YatesShuffle.html)</sup><sup> • </sup><sup>[4](https://queue.acm.org/detail.cfm?id=3664645)</sup> 奥托博尼和斯塔克建议默认使用密码学安全的伪随机数生成器，因其统计特性，并避免使用先相乘再向下取整的整数生成方法。<sup>[9](https://ar5iv.labs.arxiv.org/html/1810.10985)</sup>

**内存行为与侧信道。** 串行 Fisher–Yates 以不可预测的方式访问内存，导致缓存未命中，在基准测试中约占实际耗时的 33%（Sparc）和 80%（Pentium），且不适合外存场景；IO 块大小为 B、内存有 m 个 IO 块的机器则可以用 O(n log_m n) 次 IO 操作均匀排列 N = n · B 个条目。<sup>[20](https://www.sciencedirect.com/science/article/pii/S1570866706001031)</sup> 当排列是秘密时，朴素 Fisher–Yates 依赖于秘密的内存访问会泄露时序信息；常数时间变体以 \( O(n^2) \) 的渐近代价避免数据相关的内存访问，2024 年的一项分析提出了一种略具效率优势的自然 Fisher–Yates 变体，并发现基于 djbsort 的排序实现优于常数时间 Fisher–Yates 变体。<sup>[18](https://eprint.iacr.org/2024/008.pdf)</sup> Fisher–Yates 在列表数据结构上、在数值截断误差下以及在并行环境中也表现不佳。<sup>[13](https://algo.stat.sinica.edu.tw/hk/files/2016/11/rand-perm-2016-v2.pdf)</sup>

## References

1. [Fisher-Yates Shuffle, Wolfram MathWorld](https://mathworld.wolfram.com/Fisher-YatesShuffle.html)
2. [The Fisher-Yates shuffle (Manuel Eberl, Archive of Formal Proofs, Isabelle/HOL)](https://isa-afp.org/browser_info/current/AFP/Fisher_Yates/document.pdf)
3. [Permutation Generation Methods (Robert Sedgewick, ACM Computing Surveys)](https://www.princeton.edu/~rblee/ELE572Papers/p137-sedgewick.pdf)
4. [Zero Tolerance for Bias (ACM Queue)](https://queue.acm.org/detail.cfm?id=3664645)
5. [Richard Durstenfeld (1964). Algorithm 235: Random permutation. Communications of the ACM.](https://doi.org/10.1145/364520.364540)
6. [Correctness Proof for Fisher-Yates Shuffle (Peter J. Haas, UMass Amherst)](https://people.cs.umass.edu/~phaas/CS590M/handouts/Fisher-Yates-proof.pdf)
7. [Lecture Notes: Random shuffling and sampling (Yufei Tao, CUHK)](http://www.cse.cuhk.edu.hk/~taoyf/course/wst5017/notes/lec9.pdf)
8. [Generating Permutations at Scale (Green, Eaton, Tripathy, Nolet, Luitjens, Nvidia)](https://doi.org/10.1145/3731599.3767436)
9. [Random Sampling: Practice Makes Imperfect (Ottoboni & Stark)](https://ar5iv.labs.arxiv.org/html/1810.10985)
10. [Fisher-Yates and Sattolo's algorithm: unified analysis (CDMTCS Research Report 295, University of Auckland)](https://www.cs.auckland.ac.nz/research/groups/CDMTCS/researchreports/295mcw.pdf)
11. [Algorithms for Random Permutations (Jörg Arndt, thesis)](https://www.jjj.de/pub/arndt-rand-perm-thesis.pdf)
12. [PyTorch source: aten/src/ATen/native/cuda/Randperm.cu](https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/cuda/Randperm.cu)
13. [Generating Random Permutations by Coin-Tossing: Classical Algorithms, New Analysis and Modern Implementation (Bacher et al.)](https://algo.stat.sinica.edu.tw/hk/files/2016/11/rand-perm-2016-v2.pdf)
14. [Persi Diaconis, Mehrdad Shahshahani (1981). Generating a random permutation with random transpositions. Probability Theory and Related Fields.](https://doi.org/10.1007/bf00535487)
15. [Jeffrey S. Vitter (1985). Random sampling with a reservoir. ACM Transactions on Mathematical Software.](https://doi.org/10.1145/3147.3165)
16. [Rory Mitchell and colleagues (2022). Bandwidth-Optimal Random Shuffling for GPUs. ACM Transactions on Parallel Computing.](https://doi.org/10.1145/3505287)
17. [NIST Dataplot: SAMPLE RANDOM PERMUTATION command documentation](https://itl.nist.gov/div898/software/dataplot/refman2/auxillar/samprand.htm)
18. [Algorithms for random permutation sampling in cryptography (IACR eprint 2024/008)](https://eprint.iacr.org/2024/008.pdf)
19. [CorgiPile: Stochastic gradient descent without full data shuffle, The VLDB Journal, 2024](https://link.springer.com/article/10.1007/s00778-024-00845-0)
20. [Efficient sampling of random permutations (parallel and external-memory shuffling)](https://www.sciencedirect.com/science/article/pii/S1570866706001031)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026 · Last review: Sep 30, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/random-permutation

English original: https://www.edgepedia.org/random-permutation
