From 1ea9d45c33c1ea9a995efad16d3b177cc64053a8 Mon Sep 17 00:00:00 2001 From: lework Date: Thu, 18 Jul 2019 18:32:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E9=9A=8F=E6=9C=BA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shell/random.sh | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 shell/random.sh diff --git a/shell/random.sh b/shell/random.sh new file mode 100644 index 0000000..1c65ec8 --- /dev/null +++ b/shell/random.sh @@ -0,0 +1,121 @@ +#!/bin/bash + +#----------------------------------------------------------- +# Usage: 生成随机数 +# +#----------------------------------------------------------- + + +function TimestampRand() +{ + range=$1 + timestamp=`date +%s%N` + let res=$timestamp%$range + echo $res +} + +function RandomRand() +{ + range=$1 + let res=$RANDOM%$range + echo $res +} + +function UrandomRand() +{ + range=$1 + rand=`head -200 /dev/urandom | cksum | cut -f1 -d" "` + let res=$rand%$range + echo $res +} + +function UuidRand() +{ + range=$1 + rand=`cat /proc/sys/kernel/random/uuid| cksum | cut -f1 -d" "` + let res=$rand%$range + echo $res +} + + +function exp() +{ + echo "基于时间戳: `TimestampRand 10`" + echo "基于\$RANDOM: `RandomRand 10`" + echo "基于urandom: `UrandomRand 10`" + echo "基于uuid: `UuidRand 10`" + + str="" + for i in `seq 1 6`;do + str="$str`RandomRand 9`" + done + + echo $str + echo "字符翻转:" + echo $str | rev + + echo $str | awk '{for(i=1;i<=length;i++){line=substr($0,i,1)line}}END{print line}' +} + +$@ +[root@node130 tmp]# cat b.sh +#!/bin/bash + +#----------------------------------------------------------- +# Usage: 生成随机数 +# +#----------------------------------------------------------- + + +function TimestampRand() +{ + range=$1 + timestamp=`date +%s%N` + let res=$timestamp%$range + echo $res +} + +function RandomRand() +{ + range=$1 + let res=$RANDOM%$range + echo $res +} + +function UrandomRand() +{ + range=$1 + rand=`head -200 /dev/urandom | cksum | cut -f1 -d" "` + let res=$rand%$range + echo $res +} + +function UuidRand() +{ + range=$1 + rand=`cat /proc/sys/kernel/random/uuid| cksum | cut -f1 -d" "` + let res=$rand%$range + echo $res +} + + +function exp() +{ + echo "基于时间戳: `TimestampRand 10`" + echo "基于\$RANDOM: `RandomRand 10`" + echo "基于urandom: `UrandomRand 10`" + echo "基于uuid: `UuidRand 10`" + + str="" + for i in `seq 1 6`;do + str="$str`RandomRand 9`" + done + + echo $str + echo "字符翻转:" + echo $str | rev + + echo $str | awk '{for(i=1;i<=length;i++){line=substr($0,i,1)line}}END{print line}' +} + +$@ \ No newline at end of file