微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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().

原文地址:https://www.jb51.cc/linux/397211.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐