linux中 $RANDOM取随机数

 $RANDOM 是linux中的内置变量,可以随机生成 0~32767之间的整数数字。

1、取0~9的随机数

复制代码
[root@centos7pc1 test3]# ls
[root@centos7pc1 test3]# expr $RANDOM % 10
7
[root@centos7pc1 test3]# expr $RANDOM % 10
2
[root@centos7pc1 test3]# expr $RANDOM % 10
0
[root@centos7pc1 test3]# for i in `seq 100`; do expr $RANDOM % 10 >> a.txt; done
[root@centos7pc1 test3]# ls
a.txt
[root@centos7pc1 test3]# sort a.txt | uniq -c
     14 0
     14 1
     10 2
     12 3
      4 4
     12 5
      9 6
      6 7
     10 8
      9 9
复制代码

 

2、取1-10的随机数

复制代码
[root@centos7pc1 test3]# expr $RANDOM % 10 + 1
5
[root@centos7pc1 test3]# expr $RANDOM % 10 + 1
8
[root@centos7pc1 test3]# expr $RANDOM % 10 + 1
3
[root@centos7pc1 test3]# expr $RANDOM % 10 + 1
1
[root@centos7pc1 test3]# expr $RANDOM % 10 + 1
8
[root@centos7pc1 test3]# for i in `seq 100`; do expr $RANDOM % 10 + 1 >> a.txt; done
[root@centos7pc1 test3]# ls
a.txt
[root@centos7pc1 test3]# wc -l a.txt
100 a.txt
[root@centos7pc1 test3]# sort a.txt | uniq -c
     11 1
     14 10
     13 2
      9 3
     12 4
      8 5
      7 6
      6 7
     10 8
     10 9
复制代码

 

posted @   小鲨鱼2018  阅读(584)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示