我正在研究
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,usesrand()
.