linux_shell_生成随机整数

linux_shell_生成随机整数

转载注明来源: 本文链接 来自osnosn的博客,写于 2021-09-18.

参考

方法

  • rand=$(shuf -i 1-999 -n1)
    • 这个比较好。速度和 od 差不多。
  • rand=$(head -c6 /dev/urandom|sum |cut -f1 -d' '|sed -e 's/^0*//')
  • rand=$(head -c2 /dev/urandom |od -A n -t u2)
  • od -A n -t u2 -N2 /dev/urandom|tr -d ' '
  • rand=$(od -A n -t u2 -N2 /dev/urandom)
    • 这个比较好。速度和 shuf 差不多。
  • rand=$RANDOM
    • 速度最快,可是仅 bash 可用
  • rand=$(tr -cd "[:digit:]" < /dev/urandom | head -c 6|sed -e 's/^0*//')
  • rand=$(tr -cd 0-9 < /dev/urandom | head -c 6|sed -e 's/^0*//')
  • rand=$(date +%s%N)
  • openssl rand -hex 10
    • 输出 hex 格式
  • rand=$(date +%N|awk '{srand($1); printf "%d",rand()*1000000}')

转载注明来源: 本文链接 https://www.cnblogs.com/osnosn/p/15308304.html
来自 osnosn的博客 https://www.cnblogs.com/osnosn/ .


原文地址:https://www.cnblogs.com/osnosn/p/15308304.html