Linux Shell 生成随机数和随机字符串的方法示例

2019-09-23 08:53:28于丽

其中,参数 -base64 或 -hex 对随机字符串进行base64编码或用hex格式显示

结合 cksum 产生整数、md5sum 产生字符串,可以产生随机的整数或字符串(仅含小写字母和数字)

例如:

# openssl rand -base64 8   # 第一次执行
Vt4MNFIfzCU=
# openssl rand -base64 8   # 第二次执行, 随机数不同
uwnovaLKhek=
# openssl rand -base64 8 | cksum  # 生成随机整数
3663376449 13
# openssl rand -base64 8 | md5sum   # 生成随机字符串
1f36cf340e0a90ccb0d504925c3d7ada -
# openssl rand -base64 8 | cksum | cut -c1-8 # 截取数字
15997092
# openssl rand -base64 8 | md5sum | cut -c1-8 # 截取字符串
f1a972ce

# openssl rand -hex 8   # 第一次执行
c5bc62152bddadfb
# openssl rand -hex 8   # 第二次执行, 随机数不同
156642181b22306a
# openssl rand -hex 8 | cksum  # 生成随机整数
3663376449 13
# openssl rand -hex 8 | md5sum   # 生成随机字符串
1f36cf340e0a90ccb0d504925c3d7ada -
# openssl rand -hex 8 | cksum | cut -c1-8 # 截取数字
15997092
# openssl rand -hex 8 | md5sum | cut -c1-8 # 截取字符串
f1a972ce

示例:使用 openssl rand 生成 40,000,000~50,000,000 之间的随机数

#!/bin/bash
# mimvp.com 2016.05.10

## 5. Linux openssl
function mimvp_randnum_openssl() {
  min=$1
  max=$2
  mid=$(($max-$min+1))
  num=$(openssl rand -base64 8 | cksum | cut -f1 -d ' ')   # -base64
#  num=$(openssl rand -hex 8 | cksum | cut -f1 -d ' ')    # -hex
  randnum=$(($num%$mid+$min))    
  echo $randnum
}

function print_randnum_openssl() {
  for i in {1..10};
  do
    randnum=$(mimvp_randnum_openssl 40000000 50000000)
    echo -e "$i t $randnum"
  done
}

print_randnum_openssl

运行结果:

# sh mimvp_shell_rand.sh
1      43422505
2      40756492
3      45087076
4      43882168
5      47105153
6      45505018
7      41411938
8      48662626
9      47508094
10     41362566

6. 自定义数组生成随机数

自定义一个数组,用于生成一段特定长度(整数最长为18位)的有数字和字母组成的字符串,字符串中元素取自自定义的池子。

array=(0 1 2 3 4 5 6 7 8 9) # 自定义一个数字数组

num=${#array[*]} # 获取数组的长度(元素个数)

randnum=${array[$((RANDOM%num))]}  # 利用Linux系统默认的 $RANDOM 随机数,随机从数组选择一个元素,构成新的长度数组

示例:自定义数组生成 40,000,000~50,000,000 之间的随机数(注释有点不好看,但非常有助于理解代码哈)

#!/bin/bash
# mimvp.com 2016.05.10

## 6. custom array, 可以生成整数, 字符串
function mimvp_randnum_array() {
  NUM_LENGTH=18    # 整数的位数, 依据取值范围设定, 默认最长为18位整数(取决于正整数的范围)
  STR_ARRAY=(0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)   # 生成字符串
  STR_ARRAY=(0 1 2 3 4 5 6 7 8 9)   # 生成整数

  str_array_count=${#STR_ARRAY[@]}  # 字符串数组的元素个数, 62 = 10 + 26 + 26
#  echo "str_array_count: ${str_array_count}"

  i=1
  while [ "$i" -le "${NUM_LENGTH}" ];
  do
    randnum_array[$i]=${STR_ARRAY[$((RANDOM%str_array_count))]}
    let "i=i+1"
  done
  randnum_array_count=${#randnum_array[@]}
#  echo "randnum_array_count: ${randnum_array_count}" # NUM_LENGTH 的长度: 18
#  echo "randnum_array: ${randnum_array[@]}"      # 打印出全部数组元素, 如 B 2 y t z K c Z s N l 9 T b V w j 6

  num='1'       # 整数首位不能是0, 因此直接固定为1, 防止整数时首位为0的异常错误
  for item in ${randnum_array[@]};
  do
    num="${num}${item}"
  done
#  echo "num: $num"  # 1B2ytzKcZsNl9TbVwj6 

  min=$1
  max=$2
  mid=$(($max-$min+1))
  randnum=$(($num%$mid+$min))    
  echo $randnum
}

function print_randnum_array() {
  for i in {1..10};
  do
    randnum=$(mimvp_randnum_array 40000000 50000000)
    echo -e "$i t $randnum"
  done
}

print_randnum_array