I've been refactoring a bash script that uses the special RANDOM linux environment variable. This variable provides random integers when accessed.
From another SO question:
RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.
Here's an example of the expected output, working correctly:
ubuntu:~$ echo ${RANDOM}
19227
ubuntu:~$ echo ${RANDOM}
31030
However, when I try to replicate its usage in python I was surprised to find that it does not seem to work.
>>> import os
>>> os.environ.get('RANDOM')
(No output)
>>> os.environ.get('RANDOM')==None
True
This is quite unexpected. Obviously I can just replicate the random integer behavior I want using
random.randint(0, 32767)
But other scripts may be relying on the environment variables specific value (You can seed RANDOM by writing to it), so why can I not simply read this variable in python as expected?