1

I want to print out a random number and encrypt it, like so: echo $RANDOM | sha256sum.

I would expect this to take the standard output of echo $RANDOM and pipe it into the encryption function. However doing this twice in a row (or however many times) , gives me the same output.

Here's what I know:

  • echo $RANDOM works. I do in fact get a new number at every print.

  • Calling exec zsh to reset my shell, and then running the code generates a new random output (that keeps repeating).

This is obviously not a huge problem for, but I am really curious to find out why this is happening.

I am using Zsh. Switching to Bash also gives me different results. This seems to be Zsh-related then. I'd still like to know why. Can someone explain why I am not getting a random output?

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
s_dav
  • 25
  • 4
  • What is your shell? `zsh` or `bash`. In `bash` I get different results every time. – Romeo Ninov Jan 19 '23 at 14:34
  • I am using zsh. Switcing to bash also gives me different results. This seems to be zsh-related then. I'd still like to know why. – s_dav Jan 19 '23 at 14:38
  • It might be a bug in zsh... I tested it on bash and it works – Keltari Jan 19 '23 at 14:43
  • In the linked duplicate it's important `$( )` runs a subshell. In your case `echo $RANDOM` in `echo $RANDOM | sha256sum` is run in a subshell. The mechanism of getting identical random numbers is the same. – Kamil Maciorowski Jan 19 '23 at 15:39
  • 1
    Note that if you are actually performing cryptographic operations, you want to be very particular about what (P)RNGs you use and where, so be sure to read up on the documentation. – Frank Thomas Jan 19 '23 at 15:43
  • 1
    https://xkcd.com/221/ feels relevant. – Mokubai Jan 19 '23 at 19:09

0 Answers0