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


当前回答

也可以从awk中获取随机数

awk 'BEGIN {
   # seed
   srand()
   for (i=1;i<=1000;i++){
     print int(1 + rand() * 100)
   }
}'

其他回答

从/dev/random或/dev/urandom字符特殊文件读取是可行的方法。

这些设备在读取和设计时返回真正的随机数 帮助应用软件选择用于加密的安全密钥。这样的 随机数是从熵池中提取的 通过各种随机事件。{LDD3,乔纳森·科贝,亚历山德罗 Rubini和Greg Kroah-Hartman]

这两个文件是内核随机化的接口

void get_random_bytes_arch(void* buf, int nbytes)

它从硬件中抽取真正随机的字节,如果这样的功能是由硬件实现的(通常是),或者它从熵池中抽取(包括事件之间的时间,如鼠标和键盘中断,以及注册在SA_SAMPLE_RANDOM中的其他中断)。

dd if=/dev/urandom count=4 bs=1 | od -t d

这是可行的,但将不需要的输出从dd写入到stdout。下面的命令给出了我需要的整数。我甚至可以得到指定的随机位数,因为我需要调整位掩码给算术展开:

me@mymachine:~/$ x=$(head -c 1 /dev/urandom > tmp && hexdump 
                         -d tmp | head -n 1 | cut -c13-15) && echo $(( 10#$x & 127 ))

一个bash函数,使用perl生成n位的随机数。指定数字数或n个0的模板。

rand() {
  perl -E '$ARGV[0]||=""; $ARGV[0]=int($ARGV[0])||length($ARGV[0]); say join "", int(rand(9)+1)*($ARGV[0]?1:0), map { int(rand(10)) } (0..($ARGV[0]||0)-2)' $1
}

用法:

$ rand 3
381
$ rand 000
728

调用rand n的演示,n在0到15之间:

$ for n in {0..15}; do printf "%02d: %s\n" $n $(rand $n); done
00: 0
01: 3
02: 98
03: 139
04: 1712
05: 49296
06: 426697
07: 2431421
08: 82727795
09: 445682186
10: 6368501779
11: 51029574113
12: 602518591108
13: 5839716875073
14: 87572173490132
15: 546889624135868

演示调用rand n,对于n,一个长度在0到15之间的0模板

$ for n in {0..15}; do printf "%15s :%02d: %s\n" $(printf "%0${n}d" 0) $n $(rand $(printf "%0${n}d" 0)); done
              0 :00: 0
              0 :01: 0
             00 :02: 70
            000 :03: 201
           0000 :04: 9751
          00000 :05: 62237
         000000 :06: 262860
        0000000 :07: 1365194
       00000000 :08: 83953419
      000000000 :09: 838521776
     0000000000 :10: 2355011586
    00000000000 :11: 95040136057
   000000000000 :12: 511889225898
  0000000000000 :13: 7441263049018
 00000000000000 :14: 11895209107156
000000000000000 :15: 863219624761093

还可以使用shuf(可在coreutils中使用)。

shuf -i 1-100000 -n 1

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

如果你需要很多随机数,调用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

0到9之间的随机数。

echo $((RANDOM%10))