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 @ 2022-04-02 12:16  小鲨鱼2018  阅读(554)  评论(0编辑  收藏  举报