2

System is:

pi@titania:~ $ cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

pi@titania:~ $ uname -a Linux titania.####.net 5.10.103-v7l+ #1529 SMP Tue Mar 8 12:24:00 GMT 2022 armv7l GNU/Linux

I observe differing behavior between RANDOM and URANDOM when generating some random numbers. To wit:

pi@titania:~ $ echo $URANDOM | md5sum | head -c 20; echo;
68b329da9893e34099c7

pi@titania:~ $ echo $URANDOM | md5sum | head -c 20; echo; 68b329da9893e34099c7

pi@titania:~ $ echo $URANDOM | md5sum | head -c 20; echo; 68b329da9893e34099c7

pi@titania:~ $ echo $RANDOM | md5sum | head -c 20; echo; a5355860c5367e0dc179

pi@titania:~ $ echo $RANDOM | md5sum | head -c 20; echo; 521938e64c42075b2b92

pi@titania:~ $ echo $RANDOM | md5sum | head -c 20; echo; 03505fd360ab7ae8378d

URANDOM generates the same output in a deterministic way and RANDOM does not, returning results that differ as expected each time.

I have kinda come to want to use URANDOM where possible so this piqued my interest.

Would anyone know why this difference?

John Springer

terdon
  • 242,166
3john
  • 29
  • 1
    What is $URANDOM? How do you define it? – terdon Jun 03 '22 at 16:08
  • Well, I can see the point of the question. $URANDOM is null, whereas $RANDOM returns a value. So, that is the answer to the specific question. – 3john Jun 03 '22 at 16:27
  • But neither RANDOM (3) or (4) comments on how $RANDOM gets set. Must call the random() function? – 3john Jun 03 '22 at 16:30

1 Answers1

8

Your $URANDOM variable is empty:

$ echo '' | md5sum | head -c 20; echo;
68b329da9893e34099c7

I think you are getting confused with the bash variable RANDOM (from man bash):

      RANDOM Each time this parameter is referenced, it expands to a  random
              integer  between 0 and 32767.  Assigning a value to RANDOM ini‐
              tializes (seeds) the sequence of random numbers.  If RANDOM  is
              unset,  it  loses  its special properties, even if it is subse‐
              quently reset.

While bash and various other shells provide RANDOM as a builtin variable, I don't know of any shell that also has a similar one named URANDOM. You haven't told us what shell you are using, but based on the output you are showing, your shell also does not provide it, so all you are doing is echoing an empty string.

I suspect you are actually thinking of /dev/random and /dev/urandom, which are different things entirely. You might want to read some of these:

terdon
  • 242,166
  • Thanks for the refs. – 3john Jun 03 '22 at 16:34
  • And depending on the use-case you need to be careful, as $RANDOM is just a rather small number and not a good source when you need data, for example, for an encryption key. Hashing it will not increase entropy either. – allo Jun 03 '22 at 20:03