加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 02:59:04 所属栏目:Linux 来源:网络整理
导读:我正在研究 AWK,当我第二次使用以下命令时为什么数字总是一样的? 第一次运行: awk 'BEGIN{for(i=1;i=10;i++) print int(101*rand())}'24298515591981174815 第二次运行: awk 'BEGIN{for(i=1;i=10;i++) print int(101*rand())}'24298515591981174815 解决
我正在研究 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().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读