Indradhanush Gupta
~1 min read

Categories

Tags

TIL that bash has a builtin RANDOM that can return a random integer between 0 and 32767. This means you can easily simulate printing a random number per second with something like:

$ while true;  do echo $((RANDOM % 10)); sleep 1; done

This is pretty cool because I’ve frequently used python instead for things like these, which is easy in itself but this is way simpler!

While reading about this from man bash, I also came across SRANDOM and turns out that while RANDOM is limited to 16 bit ints only and can be seeded, SRANDOM can generate 32 bit ints and cannot be seeded. This is also very easy to verify. For example:

RANDOM=42
for i in 1 2 3; do echo $RANDOM; done
17772
26794
1435

This will give you the same output each time you run it after setting RANDOM=42 for example. But with SRANDOM the output will be different each time!

Another interesting thing I observed is that SRANDOM does not work (returns an empty output) on my OSX shell running GNU bash, version 5.2.37(1)-release (aarch64-apple-darwin24.2.0). But I was able to get it to work on a linux box.