是否每个进程都需要调用Elixir/Erlang Random/Rand Seed?

k2fxgqgv  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(157)

From Hashrockets blog post on " The Adventures of Generating Random Numbers in Erlang and Elixir " Dorian Karter mentions that opening and closing erl sessions allows him to reproduce the same output from the PRNG random:uniform(). .
Turns out random uses the same seed by default for every VM instance.
E.G:

Session one

1> random:uniform().
0.4435846174457203

Session two

1> random:uniform().
0.4435846174457203

I can't seem to find in documentation, or my searches across the net if calling the seed methods will apply to the whole VM, or to the process that calls the seed method only.
The question: In Elixir, would you need to call the seed (for both random or rand ) function per erlang process? Or does one call ~somewhere~ apply across the entire Erlang runtime?

rlcwz9us

rlcwz9us1#

rand模块会取代random模块:
将使用改进的兰德模块代替该模块。
但是,对于rand模块:
如果进程调用uniform/0、uniform/1或uniform_真实的/0而没有首先设置种子,则会使用默认算法自动调用seed/1,并创建非常数种子。
因此,使用rand:uniform/0重新启动虚拟机后,您将得到不同的结果。
但是,如果您将随机性用于加密目的,则应使用crypto:strong_rand_bytes/1

ars1skjm

ars1skjm2#

来自https://erlang.org/doc/man/random.html的文档:
在 * 进程字典 * 中生成随机数种子...并返回旧状态。
(强调我)
因此,是的,random模块要求每个流程都单独植入。

相关问题