如何在Bash中生成一个范围内的随机数?


当前回答

没有模块化的纯Bash随机数

lowerRange=10   # inclusive
upperRange=20   # exclusive

randomNumber=$(( RANDOM * ( upperRange - lowerRange) / 32767 + lowerRange ))

其他回答

这里有$RANDOM。 我不知道具体是怎么回事。但它确实有效。 对于测试,您可以执行:

echo $RANDOM

我已经采取了一些这些想法,并做出了一个函数,应该执行迅速,如果大量的随机数是必需的。

如果你需要很多随机数,调用od的代价是很高的。相反,我只调用它一次,并存储来自/dev/urandom的1024个随机数。当调用rand时,返回最后一个随机数并按比例缩放。然后从缓存中删除它。当缓存为空时,再读取1024个随机数。

例子:

rand 10; echo $RET

返回RET中0到9(含9)之间的随机数。

declare -ia RANDCACHE
declare -i RET RAWRAND=$(( (1<<32)-1 ))

function rand(){  # pick a random number from 0 to N-1. Max N is 2^32
  local -i N=$1
  [[ ${#RANDCACHE[*]} -eq 0 ]] && { RANDCACHE=( $(od -An -tu4 -N1024 /dev/urandom) ); }  # refill cache
  RET=$(( (RANDCACHE[-1]*N+1)/RAWRAND ))  # pull last random number and scale
  unset RANDCACHE[${#RANDCACHE[*]}-1]     # pop read random number
};

# test by generating a lot of random numbers, then effectively place them in bins and count how many are in each bin.

declare -i c; declare -ia BIN

for (( c=0; c<100000; c++ )); do
  rand 10
  BIN[RET]+=1  # add to bin to check distribution
done

for (( c=0; c<10; c++ )); do
  printf "%d %d\n" $c ${BIN[c]} 
done

更新:这并不适用于所有n。如果使用小n,也会浪费随机比特。注意(在这种情况下)一个32位随机数有足够的熵容纳9个0到9之间的随机数(10*9=1,000,000,000 <= 2*32),我们可以从每个32个随机源值中提取多个随机数。

#!/bin/bash

declare -ia RCACHE

declare -i RET             # return value
declare -i ENT=2           # keep track of unused entropy as 2^(entropy)
declare -i RND=RANDOM%ENT  # a store for unused entropy - start with 1 bit

declare -i BYTES=4         # size of unsigned random bytes returned by od
declare -i BITS=8*BYTES    # size of random data returned by od in bits
declare -i CACHE=16        # number of random numbers to cache
declare -i MAX=2**BITS     # quantum of entropy per cached random number
declare -i c

function rand(){  # pick a random number from 0 to 2^BITS-1
  [[ ${#RCACHE[*]} -eq 0 ]] && { RCACHE=( $(od -An -tu$BYTES -N$CACHE /dev/urandom) ); }  # refill cache - could use /dev/random if CACHE is small
  RET=${RCACHE[-1]}              # pull last random number and scale
  unset RCACHE[${#RCACHE[*]}-1]  # pop read random number
};

function randBetween(){
  local -i N=$1
  [[ ENT -lt N ]] && {  # not enough entropy to supply ln(N)/ln(2) bits
    rand; RND=RET       # get more random bits
    ENT=MAX             # reset entropy
  }
  RET=RND%N  # random number to return
  RND=RND/N  # remaining randomness
  ENT=ENT/N  # remaining entropy
};

declare -ia BIN

for (( c=0; c<100000; c++ )); do
  randBetween 10
  BIN[RET]+=1
done

for c in ${BIN[*]}; do
  echo $c
done

bash 5.1引入了一个新变量SRANDOM,它从系统的熵引擎中获取随机数据,因此它不是线性的,不能重新种子以获得相同的随机序列。这个变量可以用来代替RANDOM来生成更多的随机数。

$ echo $((1 + SRANDOM % 10))
4

生成随机的3位数字

这对于创建示例数据非常有用。示例:将所有测试数据放在名为“test-create-volume-123”的目录中,然后在测试完成后,删除整个目录。通过生成恰好三个数字,就不会有奇怪的排序问题。

printf '%02d\n' $((1 + RANDOM % 100))

这是按比例缩小的,例如到一个数字:

printf '%01d\n' $((1 + RANDOM % 10))

它可以放大,但只能放大到四位数。原因见上文:)

是什么:

perl -e 'print int rand 10, "\n"; '