RANDOM 的用法

random 用法

1、利用RANDOM取随机数

shell有一个环境变量RANDOM,范围是0--32767

如果我们想要产生0-25范围内的数:$(($RANDOM%26),在$(()) 是可以省略取值的$符号的。

用这个环境变量对26取模即可。
如果想得到4--20范围内的数 : $(($RANDOM%20+4 ))

#!/bin/bash
#RANDOM=$$

PIPS=6
MAX=10000
throw=1


one=0
two=0
three=0
four=0
five=0
five=0
six=0

count()
{
	case $1 in
		0) let "one=one+1";;
		1) let "two=two+1";;
		2) let "three=three+1";;
		3) let "four=four+1";;
		4) let "five=five+1";;
		5) let "six=six+1";;
	esac
	
	}
	
while [ $throw -le $MAX ]
do
	let "dice=RANDOM % $PIPS" 
	count $dice
	let "throw=throw + 1"
done

echo "The statistics results are as follows:"  
echo "one=$one"  
echo "two=$two"  
echo "three=$three"  
echo "four=$four"  
echo "five=$five"  
echo "six=$six"  
原文地址:https://www.cnblogs.com/sxwen/p/8000513.html