Generating random numbers to match a range in Bash

If I ran it ./random.sh 10 45

, it would only return random numbers between 10

and 45

.

I can generate a random number using

randomNumber=$((1 + RANDOM % 100))

      

but now how can I let the user specify the upper and lower limit of the random number?

+3


source to share


4 answers


You can use shuf

#!/bin/bash

# $1: Lower limit
# $2: Upper limit
# Todo  Check arguments

shuf -i $1-$2 -n 1

      



./random.sh 45 50

+2


source


Try the following (pure BASH):



   low=10
   hgh=45
   rand=$((low + RANDOM%(1+hgh-low)))

      

+1


source


The idea is to set the range at the lower border by default, for example 10

with a higher border, for example 45

. So you set up the bottom border like this: $RANDOM % 45 + 10

right?

But there is a problem with this solution, it assumes that you will always be between 0 + 10

and 45

, so it actually works until you reach 35 ( 35 + 10 = 45

your upper bound), more than 35

outside of your bounds.

The solution for staying in the range is to do this $RANDOM % (higher_b - lower_b)

, which will allow you to stay at the top border, and then add a bottom border which gives you:

$RANDOM % (45 -10) + 10

      

An example of incorrect output:

for i in {0..10};do printf $[RANDOM % 45 + 10]" ";done
47 31 53 23 36 10 22 36 11 25 54

      

An example of the right output:

for i in {0..10};do printf $[RANDOM % 35 +10]" ";done
39 44 14 12 38 31 25 13 42 33 16

      

You can also write RANDOM % (higher - lower +1)

if you want your index to include a higher score.

+1


source


The problem with modulo is that $RANDOM % N

if N is not a power of 2, it does not have an equal probability distribution for all results: How to generate a random number in Bash? ... Per man bash

, $RANDOM

produces a number between 0 and 32767 (2 ** 15-1). This may not matter much in some situations, but by slightly rewriting the expression, we get an equal distribution.

for i in {0..10}; do echo -n "$((RANDOM*36/32768 + 10)) "; done; echo

      

A bash script with a user selectable range:

#!/bin/bash
LOW=$1
HIGH=$2
echo $((RANDOM * ($HIGH-$LOW+1) / 32768 + LOW))

      

You will also want to do some parameter checking.

0


source







All Articles