RandomSample[{e1,e2,…},n]
给出 ei 的一个 n 个元素组成的伪随机采样.
RandomSample[{w1,w2,…}{e1,e2,…},n]
使用权重 wi,给出 ei 的 n 个元素组成的伪随机采样.
RandomSample[{e1,e2,…}]
给出 ei 的一个伪随机排列.
RandomSample
RandomSample[{e1,e2,…},n]
给出 ei 的一个 n 个元素组成的伪随机采样.
RandomSample[{w1,w2,…}{e1,e2,…},n]
使用权重 wi,给出 ei 的 n 个元素组成的伪随机采样.
RandomSample[{e1,e2,…}]
给出 ei 的一个伪随机排列.
更多信息
- RandomSample 亦称为简单随机抽样或不放回抽样.
- RandomSample[{e1,e2,…},n] 从不对任何 ei 取样超过一次.
- RandomSample[{e1,e2,…},n] 对每一个 ei 等概率取样.
- RandomSample[{e1,e2,…},UpTo[n]] 给出 n 个 ei 的样本,或者尽可能多.
- RandomSample[i;;j;;k,n] 可用于以步长 k 在 i 到 j 的范围内对 Span 采样.
- 无论何时运行 Wolfram 语言,RandomSample 给出伪随机选择组成的一个不同序列. 用户可以使用 SeedRandom 从一个特定的种子开始.
- SeedRandom 的一个 Method 选项可以用来指定所用的伪随机生成器.
范例
打开所有单元 关闭所有单元基本范例 (3)
范围 (4)
RandomSample[{Exp[x], x ^ 2, 1 / x, 2}]如果对于稍后的元素具有较大的权重,则稍后的元素往往更容易被选入样本:
RandomSample[Range[30] -> Range[30], 10]RandomSample[-10 ;; 10, 10]RandomSample[-10 ;; 10 ;; 2, 10]RandomSample[1 ;; 2 ^ 30, 10]应用 (1)
属性和关系 (3)
用 SeedRandom 得到重复的随机值:
{RandomSample[Range[10]], RandomSample[Range[10]]}{SeedRandom[1234];RandomSample[Range[10]], SeedRandom[1234];RandomSample[Range[10]]}用 BlockRandom 来阻止 RandomSample 的使用对其他造成影响:
{BlockRandom[RandomSample[Range[10]]], RandomSample[Range[10]]}大小为1的 RandomSample 和 RandomChoice 服从同样的分布:
Table[RandomSample[{a, b, c, d}, 1], {10 ^ 5}]//Flatten//Tally//SortRandomChoice[{a, b, c, d}, 10 ^ 5]//Tally//SortTable[RandomSample[10 ^ Range[4] -> {a, b, c, d}, 1], {10 ^ 5}]//Flatten//Tally//SortRandomChoice[10 ^ Range[4] -> {a, b, c, d}, 10 ^ 5]//Tally//Sort技术笔记
-
▪
- 随机数的生成
文本
Wolfram Research (2007),RandomSample,Wolfram 语言函数,https://reference.wolfram.com/language/ref/RandomSample.html (更新于 2015 年).
CMS
Wolfram 语言. 2007. "RandomSample." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2015. https://reference.wolfram.com/language/ref/RandomSample.html.
APA
Wolfram 语言. (2007). RandomSample. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/RandomSample.html 年
BibTeX
@misc{reference.wolfram_2026_randomsample, author="Wolfram Research", title="{RandomSample}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/RandomSample.html}", note=[Accessed: 30-October-2026]}
BibLaTeX
@online{reference.wolfram_2026_randomsample, organization={Wolfram Research}, title={RandomSample}, year={2015}, url={https://reference.wolfram.com/language/ref/RandomSample.html}, note=[Accessed: 30-October-2026]}