mercredi 4 juillet 2018

Why does piping the output of $RANDOM affect the value?

Below is a script that I expect to generate the same pair of numbers three times:

#! /usr/bin/env bash
RANDOM=5
echo " first" $RANDOM
echo "second" $RANDOM

echo

RANDOM=5
echo " first" $RANDOM
echo "second" $RANDOM

echo

RANDOM=5
echo "??????" $RANDOM | cat
echo " first" $RANDOM

Instead, piping the output prevents the random seed from being respected. Why?

 first 18499
second 9909

 first 18499
second 9909

?????? 843
 first 18499


Ultimately, I'd like to be able do something like this:

#! /usr/bin/env bash
n=3
for i in $( seq $n) ; do
    RANDOM=5
    echo $RANDOM | md5sum | cut -d' ' -f1
done

What modifications can I make to the latter script so that its output is the same each time I run it?




Aucun commentaire:

Enregistrer un commentaire