shellでランダム乱数作成($RANDOM)方法

方法1
numbers=(122 134 132 154 160 189)

while true
do
index=$(($RANDOM % ${#numbers[*]}))
echo numbers[$index]=${numbers[$index]}
sleep 1
done

方法2
while true
do
RANDOM=$((10#$(date +%N)))
echo random=$RANDOM
sleep 1
done

方法3
random ()
{
local l_min=$1
local l_max=$2
local l_div=$3
local l_range=$(($l_max – $l_min + 1))

RANDOM=`date +%N`
local number=$((($RANDOM % $l_range + $l_min + $l_div – 1) / $l_div * $l_div))

echo $number
}

update_count ()
{
case $1 in
3) ((numbers[3]++)) ;;
6) ((numbers[6]++)) ;;
9) ((numbers[9]++)) ;;
12) ((numbers[12]++)) ;;
15) ((numbers[15]++)) ;;
18) ((numbers[18]++)) ;;
21) ((numbers[21]++)) ;;
24) ((numbers[24]++)) ;;
27) ((numbers[27]++)) ;;
30) ((numbers[30]++)) ;;
esac
}

count=0
readonly max_count=600
readonly g_min=1
readonly g_max=30
readonly g_div=3

while [ $count -lt $max_count ]
do
update_count `random $g_min $g_max $g_div`

((count++))
done

echo numbers[3]=${numbers[3]}
echo numbers[6]=${numbers[6]}
echo numbers[9]=${numbers[9]}
echo numbers[12]=${numbers[12]}
echo numbers[15]=${numbers[15]}
echo numbers[18]=${numbers[18]}
echo numbers[21]=${numbers[21]}
echo numbers[24]=${numbers[24]}
echo numbers[27]=${numbers[27]}
echo numbers[30]=${numbers[30]}

shellscript

Posted by arkgame