Random Number Between Range in Shell

Random number from a range in a Bash Script

shuf -i 2000-65000 -n 1

Enjoy!

Edit: The range is inclusive.

Generating random number between 1 and 10 in Bash Shell Script

$(( ( RANDOM % 10 )  + 1 ))

EDIT. Changed brackets into parenthesis according to the comment.
http://web.archive.org/web/20150206070451/http://islandlinux.org/howto/generate-random-numbers-bash-scripting

Random number between range in shell

If you have tr, head and /dev/urandom, you can write this:

tr -cd 0-9 </dev/urandom | head -c 3

Then you have to use the remainder operator to put in 0-60 range.

Random number from a range in a Bash Script

shuf -i 2000-65000 -n 1

Enjoy!

Edit: The range is inclusive.

Generate random number except values in bash

Practical General Solution

Instead of specifying a range of numbers you can specify each valid number individually using the -e option (at least for shuf from GNU coreutils) or you can specify the numbers in an (anonymous) file. Since the last option is more portable (works also for shuffle on BSD), we will use that. Now we only have to generate output with the numbers 1, 2, 4, 5, 6, 7, 9. Here are some examples:

shuf -n 1 <(seq 9 | grep -Fxv -e{3,8})

shuf -n 1 <(printf %s\\n 1 2 4 5 6 7 9)

shuf -n 1 <(printf %s\\n 1 2 {4..7} 9)

shuf -n 1 <(seq 2; seq 4 7; echo 9)

Just For Fun

For each finite set S={i1,i2,…in}⊂ℕ of integers you can come up (by hand) with a polynomial f such that f(x)=ix for all x∈{0,1,…,n-1}.

In general, polynomial f has degree n-1. Sometimes we can use rounding to reduce the degree. In your concrete case of S={1,2,4,5,6,7,9} we can use f(x) = floor(1.25x+1.5). Here is an implementation in bash using bc (the (…) / 1 rounds down).

bc <<< "($RANDOM % 7 * 1.25 + 1.5) / 1"

A benefit of this method is that it works purley with built-ins if we scale all numbers inside the formula such that they become integers (here we scaled by a factor of 4).

echo $(( ($RANDOM % 7 * 5 + 6) / 4 ))

How To Generate a Random Number that is a multiple 3 in a Range in a Bash Script?

Bash has a builtin RANDOM (0 ~ 32767) var. If you have Bash 5.1+, there's also SRANDOM (0 ~ 4294967295).

[Bash-5.2] % echo $(( 3 * (15 + RANDOM % 10) ))
51
[Bash-5.2] % echo $(( 3 * (15 + RANDOM % 10) ))
57
[Bash-5.2] % echo $(( 3 * (15 + RANDOM % 10) ))
48

How to use random number in shell script?

Each time you read the value of the variable $RANDOM,
it gives you a random number between 0 and 2^15 - 1,
that is 0 and 32767. So that doesn't give you enough range.
You could use two $RANDOM as two digits of base-15,
and then take appropriate modulo and apply appropriate range normalization.

Here's the logic wrapped in a function:

randrange() {
min=$1
max=$2

((range = max - min))

((maxrand = 2**30))
((limit = maxrand - maxrand % range))

while true; do
((r = RANDOM * 2**15 + RANDOM))
((r < limit)) && break
done

((num = min + r % range))
echo $num
}

And then you can generate the files in a loop like this:

for i in 01 02 03 04 05; do
echo "AArandom=$(randrange 50000 150000)" > $i.dat
done

Note that there is a caveat in the implementation of randrange:
there is a loop to re-roll in case the value would be biased,
but theoretically this may prevent the function from terminating.
In practice, that's extremely unlikely, but deserves a mention.



Related Topics



Leave a reply



Submit