bash 中 $RANDOM 环境变量的种子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42004870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Seed for $RANDOM environment variable in bash
提问by Juan
I am working on a bash script that uses the $RANDOM
environmental variable as an input in a simulation. The variable does what it say, give random integers, and I as far I understand it is taken from /dev/random
.
我正在编写一个使用$RANDOM
环境变量作为模拟输入的 bash 脚本。该变量按照它所说的做,给出随机整数,据我所知,它取自/dev/random
.
However I would like to have a reproducible simulation, then the pseudo-random generator should be initialized with a seed; is it possible to have a seed for the $RANDOM
variable in bash?
但是我想要一个可重复的模拟,那么伪随机生成器应该用种子初始化;是否有可能$RANDOM
在 bash 中为变量提供种子?
回答by chepner
From the man page:
从手册页:
RANDOM Each time this parameter is referenced, a random integer between
0 and 32767 is generated. The sequence of random numbers may be
initialized by assigning a value to RANDOM. If RANDOM is unset,
it loses its special properties, even if it is subsequently
reset.
Note that assigning a value to RANDOM
actually seeds it; the assigned value won't be the next value returned.
请注意,为RANDOM
实际种子分配一个值;分配的值不会是下一个返回的值。
$ RANDOM=1341
$ echo $RANDOM $RANDOM $RANDOM
26571 16669 28842
$ echo $RANDOM $RANDOM $RANDOM
14953 18116 2765
$ RANDOM=1341
$ echo $RANDOM $RANDOM $RANDOM
26571 16669 28842
$ echo $RANDOM $RANDOM $RANDOM
14953 18116 2765