How can I get a random number in a UNIX shell script???

From: Dr. David Kirkby (drkirkby@ntlworld.com)
Date: 04/18/03


From: "Dr. David Kirkby" <drkirkby@ntlworld.com>
Date: Fri, 18 Apr 2003 18:39:42 +0100

I've written a UNIX shell script
http://www.medphys.ucl.ac.uk/~davek/teston

that allows me to copy a software package to a remote machine, then
try to configure and build the software on that remote host. So I
might do something like this, to check package package-foo.tar.gz on 3
machines with 4 different configuration options, with the -T option
saying to do this is a unique directory.

teston host1 -T package-foo
teston host1 -c '--with-threads' -T package-foo
teston host1 -c '--with-mpi' -T package-foo
teston host1 -c '--without-gsl' -T package-foo

teston host2 -T package-foo
teston host2 -c '--with-threads' -T package-foo
teston host2 -c '--with-mpi' -T package-foo
teston host2 -c '--without-gsl' -T package-foo

teston host3 -T package-foo
teston host3 -c '--with-threads' -T package-foo
teston host3 -c '--with-mpi' -T package-foo
teston host3 -c '--without-gsl' -T package-foo

so package-foo.tar.gz gets configure/built on 3 hosts, with 4
different options to the configure script, in ****hopefully**** 4
different directories on each host.

My problem is that I need to put each set of file into a separate
directory, as otherwise they mess each other up. Currently I'm using
as a directory $HOME/$REMOTE_HOSTNAME/$PID, where $PID is the PID on
the local machine from where this is started. There is no risk of
files from host1 interfering with those of host2, since they are in
different directories. But there is nothing preventing those with each
different configuration option screwing each other up on the same
host, as I can't guarantee the PIDs will be different each time.

Hence I want a random number that is portable, so not relying on perl
or /dev/random. It doesn't need to by crytograpically secure, but
hopefully the risk of it producing the same number on the one system
is very small (say < 1 in 10^5).

I don't want to use the systems 'rand' (or similar) and seed it from
the system's clock, since if these scripts are run in parallel (which
is what I would do on a multi-processor machine), there is every
chance that they will be seeded with the same number, as they would
start at the same time.

Any thoughts? Perhaps the PID is reasonable, given the consequences
for it going wrong are not that huge, but I'd like to make it a bit
more robust, if only for peace of mind.

-- 
Dr. David Kirkby,
Senior Research Fellow,
Department of Medical Physics,
University College London,
11-20 Capper St, London, WC1E 6JA.
Tel: 020 7679 6408 Fax: 020 7679 6269
Internal telephone: ext 46408
e-mail davek@medphys.ucl.ac.uk