samedi 20 février 2021

Generate a random number between 0 and 10 in bash

I was recently looking for a random number generator for my script which would include both 0 and 10. While trying various examples this one seems to work for me.

num=$(( $RANDOM % 11 ))

Example:

arr=()

for (( i = 0; i <= 50; i++ ))
do
  num=$(( ( $RANDOM % 11 ) ))
  arr+=($num)
done

echo ${arr[@]}

Output: 6 7 1 0 10 4 5 3 1 6 3 9 3 3 4 8 10 0 5 6 2 1 1 2 9 9 0 3 7 2 5 8 0 7 2 1 2 1 7 5 0 5 8 5 0 1 7 9 8 8 0




Aucun commentaire:

Enregistrer un commentaire