Random number and script

What im doing wrong so every time i get same numbers

daemon.notice procd: /etc/rc.d/S95done: 1?= 10...
kern.notice kernel: [ 112.665141] random: crng init done


while true; do
initiald=$((RANDOM % 1000 + 1000))
echo "1= $initiald..
sleep $initiald
command

delay2=$((RANDOM % 100 + 100))
echo "2?= $delay2..."
sleep $delay2
command

done

exit 0


apparently random doesn't work at all,

it only works for me inside PUTTY when I switch to BASH and test commands, everything is ok

in what way would you modify this so that it can be started via startup

Yeah, RANDOM is bash-specific, so with a POSIX shell you can use hexdump of /dev/urandom bytes. Change the math to suit your use case (this version gives a 0-59 value from a random byte 0-255, so distribution is not exactly uniform, but close enough):

$ delay=$(($(hexdump -n 1 -e '"%u"' </dev/urandom) % 60))
$ echo $delay
37
1 Like

the word POSIX in the answer helped me much more than anything,
at least now i know which shell it is or whatever it is :smiley:
i managed to solve it using

delay1=$(shuf --random-source='/dev/urandom' -n 1 -i 99-9999)

1 Like

Just for the record, the default shell on the router is Busybox ash, so pretty much POSIX plus a few extensions (for example, you'll see widespread use of non-POSIX local all over).

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.