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?
2条答案
按热度按时间rlcwz9us1#
rand
模块会取代random
模块:将使用改进的兰德模块代替该模块。
但是,对于
rand
模块:如果进程调用uniform/0、uniform/1或uniform_真实的/0而没有首先设置种子,则会使用默认算法自动调用seed/1,并创建非常数种子。
因此,使用
rand:uniform/0
重新启动虚拟机后,您将得到不同的结果。但是,如果您将随机性用于加密目的,则应使用
crypto:strong_rand_bytes/1
。ars1skjm2#
来自https://erlang.org/doc/man/random.html的文档:
在 * 进程字典 * 中生成随机数种子...并返回旧状态。
(强调我)
因此,是的,
random
模块要求每个流程都单独植入。