linux – 为什么AWK中的随机数在第一次运行后没有变化?

前端之家收集整理的这篇文章主要介绍了linux – 为什么AWK中的随机数在第一次运行后没有变化?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在研究 AWK,当我第二次使用以下命令时为什么数字总是一样的?

第一次运行:

awk 'BEGIN{for(i=1;i<=10;i++) print int(101*rand())}'
24
29
85
15
59
19
81
17
48
15

第二次运行:

awk 'BEGIN{for(i=1;i<=10;i++) print int(101*rand())}'
24
29
85
15
59
19
81
17
48
15

解决方法

https://www.gnu.org/software/gawk/manual/html_node/Numeric-Functions.html

CAUTION: In most awk implementations,including gawk,rand() starts generating numbers from the same starting number,or seed,each time you run awk. Thus,a program generates the same results each time you run it. The numbers are random within one awk run but predictable from run to run. This is convenient for debugging,but if you want a program to do different things each time it is used,you must change the seed to a value that is different in each run. To do this,use srand().

猜你在找的Linux相关文章