Shell by Example: Random Numbers Bash

Bash

Random numbers can be generated using the $RANDOM variable. This is not cryptographically secure, but it is fast and easy to use. To generate a cryptographically secure random number, use /dev/urandom.

$RANDOM - Bash built-in (0 to 32767):

Edit
#!/bin/bash
echo "Bash \$RANDOM: $RANDOM"
echo "Another: $RANDOM"
echo "Range 1-100: $((RANDOM % 100 + 1))"
Output:
Bash $RANDOM: 579
Another: 32207
Range 1-100: 93

Bash

Random selection from array:

Edit
#!/bin/bash
fruits=("apple" "banana" "cherry" "date" "elderberry")
random_index=$((RANDOM % ${#fruits[@]}))
echo "Random fruit: ${fruits[$random_index]}"
Output:
Random fruit: elderberry

« Spawning Processes