Lord of Strandhill Lord of Strandhill - 1 month ago 9
Linux Question

Bash Generate a random number between a positive and a negative

I have to write a Program, which shows some words on an image which are randomly rotated. I would like these to be between e.g -30 and 30 ° . But I didn't found any command in bash/UNIX, that could generate a random number between a positive and a negative.

Thanks for your help.

Answer

The correct approach (no external processes are spawned) was already presented by @Barmar in a comment.

Should the OP need it unraveled, here it is:

angle=$(($RANDOM%61-30))

but I'd like to suggest a worse alternative, as someone said "Worse is better"

angle=$(seq -30 30 | shuf -n 1) 
  • seq puts to stdout a sequence of integers, newline separated,
  • shuf shuffles randomly its input,
    • the -n 1 option means output just the first randomly shuffled line,

so that we have that we generate a deck of cards, numbered -30+30, we shuffle the deck and we deal just the 1st card.

What I like in my answer is that the range of the solution is stated more clearly, at least from the POV of a non mathematically endowed person.

On the contrary it is SLOWER than using $RANDOM

10:31 boffi@debian:~ $ time for i in {1..10000} ; do echo > /dev/null ; done

real    0m0.077s
user    0m0.036s
sys     0m0.040s
10:32 boffi@debian:~ $ time for i in {1..10000} ; do echo $(($RANDOM%61-30)) > /dev/null ; done

real    0m0.138s
user    0m0.116s
sys     0m0.020s
10:32 boffi@debian:~ $ time for i in {1..10000} ; do echo $(seq -30 30|shuf -n 1) > /dev/null ; done

real    0m33.328s
user    0m0.372s
sys     0m4.560s
10:33 boffi@debian:~ $ 

(on the other hand, it takes just 3.3 ms, if you use it ONCE in your script… :-)

Comments