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


当前回答

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

echo $RANDOM

其他回答

一个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

这是我写的一个函数,它将输出一个期望范围>的随机数

描述:

随机<min> <max> 生成一个从最小到最大的随机数。最小值和最大值都可以 正数或负数,生成的随机数也可以是负数,所以 只要范围(max - min + 1)小于或等于32767。Max必须是>= min。

它的核心是:

random() {
    min="$1"
    max="$2"
    
    range=$((max - min + 1))
    rand=$((min + (RANDOM % range)))
    echo "$rand"
}

用法:

# general form: obtain a random number between min and max, inclusive
random <min> <max>

# Example: obtain a random number from -10 to 10, inclusive
random -10 10

这是从bash内置变量RANDOM中工作的,它可能只在底层使用C rand(),因为它们的最大值都是32767——请参阅:

https://en.cppreference.com/w/c/numeric/random/rand https://en.cppreference.com/w/c/numeric/random/RAND_MAX

有关bash文档,请参阅man bash:

随机 每次引用此参数时,都会生成一个0到32767之间的随机整数。随机数序列可以通过赋值给random来初始化。如果RANDOM未被设置,它将失去其特殊属性,即使随后被重置。

健壮的、可运行的、可源代码的脚本版本

这是上面随机函数的一个更健壮的版本。它包括完整的错误检查,边界检查,通过random——help或random -h提供的帮助菜单,以及一个特殊的run_check功能,允许您源或运行此脚本,以便您可以源它以将随机函数导入任何其他脚本——就像您在Python中所做的一样!

random.sh <——点击这个链接总是从我的eRCaGuy_dotfiles repo中获得最新版本。

RETURN_CODE_SUCCESS=0
RETURN_CODE_ERROR=1

HELP_STR="\
Generate a random integer number according to the usage styles below.

USAGE STYLES:
    'random'
        Generate a random number from 0 to 32767, inclusive (same as bash variable 'RANDOM').
    'random <max>'
        Generate a random number from 0 to 'max', inclusive.
    'random <min> <max>'
        Generate a random number from 'min' to 'max', inclusive. Both 'min' and 'max' can be
        positive OR negative numbers, and the generated random number can be negative too, so
        long as the range (max - min + 1) is less than or equal to 32767. Max must be >= min.

This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
"

print_help() {
    echo "$HELP_STR" | less -RFX
}

# Get a random number according to the usage styles above.
# See also `utils_rand()` in utilities.c:
# https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/utilities.c#L176
random() {
    # PARSE ARGUMENTS

    # help menu
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        print_help
        exit $RETURN_CODE_SUCCESS
    fi

    # 'random'
    if [ $# -eq 0 ]; then
        min=0
        max="none"
    # 'random max'
    elif [ $# -eq 1 ]; then
        min=0
        max="$1"
    # 'random min max'
    elif [ $# -eq 2 ]; then
        min="$1"
        max="$2"
    else
        echo "ERROR: too many arguments."
        exit "$RETURN_CODE_ERROR"
    fi

    # CHECK FOR ERRORS

    if [ "$max" = "none" ]; then
        rand="$RANDOM"
        echo "$rand"
        exit "$RETURN_CODE_SUCCESS"
    fi

    if [ "$max" -lt "$min" ]; then
        echo "ERROR: max ($max) < min ($min). Max must be >= min."
        exit "$RETURN_CODE_ERROR"
    fi

    # CALCULATE THE RANDOM NUMBER

    # See `man bash` and search for `RANDOM`. This is a limitation of that value.
    RAND_MAX=32767

    range=$((max - min + 1))
    if [ "$range" -gt "$RAND_MAX" ]; then
        echo "ERROR: the range (max - min + 1) is too large. Max allowed = $RAND_MAX, but actual" \
             "range = ($max - $min + 1) = $range."
        exit "$RETURN_CODE_ERROR"
    fi

    # NB: `RANDOM` is a bash built-in variable. See `man bash`, and also here:
    # https://stackoverflow.com/a/1195035/4561887
    rand=$((min + (RANDOM % range)))
    echo "$rand"
}

# Set the global variable `run` to "true" if the script is being **executed** (not sourced) and
# `main` should run, and set `run` to "false" otherwise. One might source this script but intend
# NOT to run it if they wanted to import functions from the script.
# See:
# 1. *****https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/bash/argument_parsing__3_advanced__gen_prog_template.sh
# 1. my answer: https://stackoverflow.com/a/70662049/4561887
# 1. https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/bash/check_if_sourced_or_executed.sh
run_check() {
    # This is akin to `if __name__ == "__main__":` in Python.
    if [ "${FUNCNAME[-1]}" == "main" ]; then
        # This script is being EXECUTED, not sourced
        run="true"
    fi
}

# ----------------------------------------------------------------------------------------------------------------------
# Main program entry point
# ----------------------------------------------------------------------------------------------------------------------

# Only run main function if this file is being executed, NOT sourced.
run="false"
run_check
if [ "$run" == "true" ]; then
    random "$@"
fi

生成随机的3位数字

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

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

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

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

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

请参阅$RANDOM:

$RANDOM是一个内部Bash函数 (不是常量)返回 范围内的伪随机整数 0 - 32767。它不应该被用来 生成加密密钥。

0到9之间的随机数。

echo $((RANDOM%10))