var=$RANDOM
creates random numbers but how can i specify a range like between 0 and 12 for instance?
Example: echo $(($RANDOM%10)) #This will display a random integer between 0 and 9. echo $(($RANDOM%11)) #This will display a random integer between 0 and 10.
$RANDOM is an internal Bash function (not a constant) that returns a pseudorandom integer in the range 0 - 32767. $RANDOM should not be used to generate an encryption key. Jipe points out a set of techniques for generating random numbers within a range. # Generate random number between 6 and 30.
If you already have your random number, you can say
var=$RANDOM
var=$[ $var % 13 ]
to get numbers from 0..12
.
Edit:
If you want to produce numbers from $x
to $y
, you can easily modify this:
var=$[ $x + $var % ($y + 1 - $x) ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With