$RANDOM  –  Random Integer

$RANDOM expands to a random integer in the range [0, 32767]. Assigning a values seeds the random name generator.

The random generator generates the same numbers in subshells unless you seed it.

Examples

Seed random generator and print random value
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# without seeding, the same numbers are generated in subshells
echo "subshell 1: $(echo $RANDOM)"
echo "subshell 2: $(echo $RANDOM)"
  
# seed with "seconds since epoch" followed by "nanoseconds" to enforce different values
RANDOM=$(date +%s%N)
echo "after seeding: subshell 1: $(echo $RANDOM)"
sleep .1
RANDOM=$(date +%s%N)
echo "after seeding: subshell 2: $(echo $RANDOM)"
  
Output:
1
2
3
4
subshell 1: 26215
subshell 2: 26215
after seeding: subshell 1: 23977
after seeding: subshell 2: 1130
BashSupport Pro is a Zsh IDE with support for $RANDOMtry it now!
© 2020–2024 Joachim Ansorg
Imprint
Privacy Policy
Legal