SeedRandom[s]
重设伪随机生成器,用 s 为种子.
用时钟时刻和当前的 Wolfram 系统进程的某种属性设置伪随机数生成器的种子.
SeedRandom
SeedRandom[s]
重设伪随机生成器,用 s 为种子.
用时钟时刻和当前的 Wolfram 系统进程的某种属性设置伪随机数生成器的种子.
更多信息和选项
- 可以用 SeedRandom[s] 确定不同时刻获得相同的一个伪随机序列.
- SeedRandom 影响 RandomInteger、RandomReal、RandomComplex、RandomPrime、RandomChoice、RandomSample 和 RandomVariate.
- 种子 s 的可能的设置包括:
-
RandomGeneratorState[…] RandomGeneratorState 对象 n 整数 "seed" 字符串 Automatic 根据选项自动选择是否重新播种 - SeedRandom 返回 RandomGeneratorState,确定性生成器可以用其作为种子以重现随机序列.
- SeedRandom[Method->"method"] 指定伪随机数生成器应使用的方法. 如果之前使用了 "method",则这种形式不会重新播种.
- SeedRandom[n,Method->"method"] 重设生成器,指定使用的种子和方法.
- SeedRandom[n] 仅影响当前的生成器;SeedRandom[n,Method->All] 影响所有生成器.
- 典型的方法有:
-
"Congruential" 线性同余数生成程序(较低质量的随机性) "ExtendedCA" 扩充的元胞自动生成器(缺省) "OpenSSL" OpenSSL 加密安全生成器 "MersenneTwister" Mersenne twister 移位寄存器发生器 "MKL" Intel MKL 生成器(基于 Intel 系统) "Rule30CA" Wolfram Rule 30 生成器 - 不同的方式下给出不同级随机性的序列.
范例
打开所有单元 关闭所有单元基本范例 (1)
用 SeedRandom 产生可重复的随机数:
SeedRandom[1234];RandomReal[]SeedRandom[1234];RandomReal[]范围 (3)
SeedRandom["password"];RandomReal[]rgs = SeedRandom[]RandomReal[1, 5]用 SeedRandom 返回的 RandomGeneratorState 对象重新播种:
SeedRandom[rgs]RandomReal[1, 5]SeedRandom 影响所有随机生成器:
SeedRandom[4567];{RandomInteger[10], RandomReal[]}SeedRandom[4567];{RandomReal[], RandomInteger[10]}选项 (2)
应用 (2)
SeedRandom[1234]; Eigenvalues[RandomReal[1, {100, 100}], 1]SeedRandom[1234]; Eigenvalues[RandomReal[1, {100, 100}], -1]SeedRandom[1234];Eigenvalues[RandomReal[1, {100, 100}]][[{1, -1}]]Options[rplot] = {PlotPoints -> 100, RandomSeed -> 1234};rplot[f_, {x_, xmin_, xmax_}, OptionsPattern[]] := Module[{n = OptionValue[PlotPoints], s = OptionValue[RandomSeed]},
BlockRandom[SeedRandom[s];
ListPlot[Table[{x, f}, {x, RandomReal[{xmin, xmax}, n]}]]]]p100 = rplot[Sin[x], {x, 0, 10π}]rplot[Sin[x], {x, 0, 10π}, PlotPoints -> 125]属性和关系 (4)
SeedRandom 返回具有新的状态的 RandomGeneratorState 对象:
SeedRandom[]当用 RandomGeneratorState 对象作为种子时,忽略显式的 Method 选项:
SeedRandom[RandomGeneratorState[{"ExtendedCA", {80, 4, 0}},
{{RawArray["UnsignedInteger64", {2137, 17811601035166618301, 1074886304135541815,
10839886105759978540, 5680751118638851723, 3721304837657701207, 12648882479676994813,
76919680177341524 ... 30824604546838294, 15592153151670727127, 324171737895129758,
11862034057572822428, 6654485145575527276, 5854470426309544231, 11331002485321865147,
7991838028202245200}], 4, 0}}, RawArray["UnsignedInteger64", {10048184177401884856, 0}]], Method -> "Rule30CA"]可以从 $RandomGeneratorState 获得播种之前的状态:
state = $RandomGeneratorStateRandomReal[1, 5]SeedRandom[];RandomReal[1, 5]SeedRandom[state];RandomReal[1, 5]BlockRandom 实际上保存了随机生成器的状态并在计算结束时将其恢复:
{SeedRandom[123];RandomReal[], SeedRandom[123];RandomReal[]}{SeedRandom[123];BlockRandom[RandomReal[]], RandomReal[]}相关指南
-
▪
- 随机数的生成 ▪
- GPU 计算 ▪
- NVIDIA GPU 计算 ▪
- Apple GPU 计算
历史
1988年引入 (1.0) | 在以下年份被更新:2007 (6.0) ▪ 2010 (8.0) ▪ 2020 (12.2)
文本
Wolfram Research (1988),SeedRandom,Wolfram 语言函数,https://reference.wolfram.com/language/ref/SeedRandom.html (更新于 2020 年).
CMS
Wolfram 语言. 1988. "SeedRandom." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2020. https://reference.wolfram.com/language/ref/SeedRandom.html.
APA
Wolfram 语言. (1988). SeedRandom. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/SeedRandom.html 年
BibTeX
@misc{reference.wolfram_2026_seedrandom, author="Wolfram Research", title="{SeedRandom}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/SeedRandom.html}", note=[Accessed: 01-December-2026]}
BibLaTeX
@online{reference.wolfram_2026_seedrandom, organization={Wolfram Research}, title={SeedRandom}, year={2020}, url={https://reference.wolfram.com/language/ref/SeedRandom.html}, note=[Accessed: 01-December-2026]}