shell实例(九) ---随机数

本文介绍了四个使用Shell脚本生成随机数的实例,包括在指定范围内生成随机数、模拟掷骰子、重新设置随机数种子以及使用awk生成伪随机数。通过这些实例,展示了如何在Shell中灵活运用随机数功能。

1.两个指定值之间的随机数

#! /bin/sh
randomBetween() {
 syntax() {
   echo
   echo "Syntax: randomBetween [min] [max] [multiple] "
   echo
   echo "Expects up to 3 passed parameters, but all are completely optional."
   echo "min is the minimum value"
   echo "max is the maximum value"
   echo "multiple specifies that the answer must be a multiple of this value."
   echo "   i.e answer must be evenly divisible by this number."
   echo
   echo "If any value is missing,defaults area supplied as : 0 32767 1"
   echo "Successful completion returns 0,unsuccessful completion returns."
   echo "function syntax and 1."
   echo "The answer is returned in the global variable random Between Answer"
   echo "Negative values for any passed parameter are handled correctly."
}

local min={1:-0}
local max={2:-32767}
local divisibleBy=${3:-1}

local x
local spread

[ ${divisibleBy} -lt 0 ] && divisibleBy=$((0-divisibleBy))

if [ $# -gt 3 -o ${divisibleBy} -eq 0 -o ${min} -eq ${max} ]
then
  systax
  return 1
fi

if [ ${min} -gt ${max} ]
then
  x=${min}
  min=${max}
  max=${x}
fi

if [ $((min/divisibleBy*divisibleBy)) -ne ${min} ]
then
  if [ ${min} -lt 0 ]
  then
    min=$((min/
[root@localhost yjg]# vi 9-27.sh

 syntax() {
   echo
   echo "Syntax: randomBetween [min] [max] [multiple] "
   echo
   echo "Expects up to 3 passed parameters, but all are completely optional."
   echo "min is the minimum value"
   echo "max is the maximum value"
   echo "multiple specifies that the answer must be a multiple of this value."
   echo "   i.e answer must be evenly divisible by this number."
   echo
   echo "If any value is missing,defaults area supplied as : 0 32767 1"
   echo "Successful completion returns 0,unsuccessful completion returns."
   echo "function syntax and 1."
   echo "The answer is returned in the global variable random Between Answer"
   echo "Negative values for any passed parameter are handled correctly."
}

local min={1:-0}
local max={2:-32767}
local divisibleBy=${3:-1}

local x
local spread

[ ${divisibleBy} -lt 0 ] && divisibleBy=$((0-divisibleBy))

if [ $# -gt 3 -o ${divisibleBy} -eq 0 -o ${min} -eq ${max} ]
then
  systax
  return 1
fi

if [ ${min} -gt ${max} ]
then
  x=${min}
  min=${max}
  max=${x}
fi

if [ $((min/divisibleBy*divisibleBy)) -ne ${min} ]
then
  fi
  if [ ${min} -lt 0 ]
  then
    min=$((min/divisibleBy*divisibleBy))
  else
    min=$((((min/divisibleBy)+1)*divisibleBy))
  fi
fi

if [ $((max/divisibleBy*divisibleBy)) -ne ${max} ]
then
  if [ ${max} -lt 0 ]
  then
    max=$((((max/divisibleBy)-1)*divisibleBy))
  else
   max=$((max/divisibleBy*divisibleBy))
  fi
fi

spread=$((max-min))
[ ${spread} -lt 0 ] && spread=$((0-spread))
let spread+=divisibleBy
randomBetweenAnswer=$(((RANDOM%spread)/divisibleBy*divisibleBy+min))
return 0
}

min=-14
max=20
divisibleBy=3

declare -a answer
minimum=${min}
maximum=${max}
if [ $((minimum/divisibleBy*divisibleBy)) -ne ${minimum} ]
then
  if [ ${minimum} -lt 0 ]
  then
    minimum=$((minimum/divisibleBy*divisibleBy))
  else
    minimum=$((((minimum/divisibleBy)+1)*divisibleBy))
  fi
fi

if [ $((maximum/divisibleBy*divisibleBy)) -ne ${maximum} ]
then
  if [ ${maximum} -lt 0 ]
  then
    maximum=$((((maximum/divisibleBy)-1)*divisibleBy))
  else
    maximum=$((maximum/divisibleBy*divisibleBy))
  fi
fi

displacement=$((0-minimum))
for (( i=${minimum}; i<=${maximum}; i+=divisibleBy))
do
 answer[i+displacement]=0
done

loopIt=1000
for ((i=0; i<${loopIt}; ++i))
do
  randomBetween ${max} ${min} ${divisibleBy}
  [ ${randomBetweenAnswer} -lt ${min} -o ${randomBetweenAnswer} -gt ${max} ] && echo MIN or MAX error --- ${randomBetweenAnswer}!
  [ ${randomBetweenAnswer%${divisibleBy})) -ne 0 ] && echo DIVISIBLE By error -- ${randomBetweenAnswer}!
  answer[randomBetweenAnswer+displacement]=$((answer[randomBetweenAnswer+displacement]+1))
done

for ((i=${minimum}; i<=${maximum}; i+=divisibleBy))
do
  [ ${answer[i+displacement]} -eq 0 ] && echo "We never got an answer of $i." || echo "${i} occurred ${answer[i+displacement]} times
."
done

exit 0

 

2.用随机数来摇单个骰子

#! /bin/sh
RANDOM=$$
PIPS=6
MAXTHROWS=6
throw=0

ones=0
twos=0
threes=0
fours=0
fives=0
sixes=0

print_result(){
 echo
 echo "ones = $ones"
 echo "twos = $twos"
 echo "threes = $threes"
 echo "fours = $fours"
 echo "fives = $fives"
 echo "sixes = $sixes"
 echo
}

update_count(){
 echo "update_count: $1"
 case "$1" in
 0) let "ones += 1";;
 1) let "twos += 1";;
 2) let "threes += 1";;
 3) let "fours += 1";;
 4) let "fives += 1";;
 5) let "sixes += 1";;
esac
}

echo

while [ "$throw" -lt  "$MAXTHROWS" ]
do
 let "die1 = RANDOM % $PIPS"
 update_count $die1
 let "throw += 1"
 print_result
done

#print_result
exit 0

 

3.重新分配随机数种子


#! /bin/sh
MAXCOUNT=25
random_numbers(){
count=0
while [ "$count" -lt "$MAXCOUNT" ]
do
 number=$RANDOM
 echo -n "$number "
 let "count += 1"
done
}

echo;echo

RANDOM=1
random_numbers
echo;echo

RANDOM=1
random_numbers

echo;echo

RANDOM=2
random_numbers

echo;echo
SEED=$(head -l /dev/urandom | od -N 1 | awk '{ print $2 }')
RANDOM=$SEED
random_numbers

echo;echo
exit 0

 

4.使用awk来产生伪随机数

#! /bin/sh
AWKSCRIPT=' { srand(); print rand() } '    --------srand()是生成伪随机数函数
echo -n "Random number between 0 and 1 = "
echo | awk "$AWKSCRIPT"
exit 0

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值