Why is Bash’s $RANDOM not seeded (?) on some machines?

為{幸葍}努か 提交于 2019-12-07 05:17:47

问题


I noticed that on some machines (all openSUSE 11.2 on identical hardware) every Bash shell would output the same sequence of values for $RANDOM:

$ bash -c 'for i in `seq 10`; do echo -n "$RANDOM "; done; echo'
17767 9158 6249 18547 23633 23807 5194 22764 7977 31949 
$ bash -c 'for i in `seq 10`; do echo -n "$RANDOM "; done; echo'
17767 9158 6249 18547 23633 23807 5194 22764 7977 31949

The sequence is the same across all these machines. It seems that the random number generator is not seeded? Why does it happen and how to fix it?

On my personal machine the numbers are different every time I call above command.


回答1:


You can just seed it:

bash -c 'RANDOM=$$; for i in `seq 10`; do echo -n "$RANDOM "; done; echo'

bash -c 'RANDOM=`date +%s`; for i in `seq 10`; do echo -n "$RANDOM "; done; echo'


来源:https://stackoverflow.com/questions/5290837/why-is-bash-s-random-not-seeded-on-some-machines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!