[Gllug] Obscure question with rand() in awk, maybe a better way

James Goldwater james at eccehomo.co.uk
Wed Jun 30 12:01:38 UTC 2004


Hi,

I've been lurking and have picked up quite a few useful tips from this 
group, and hopefully I'll be able to come to the next meeting.  But in 
the meantime I've got a silly question, which someone might be able to 
answer for me.

I've got an awk script which should be swapping a line in a file for a 
_random_ line from another file. (It's a stupid quote generator). 
Everything easy works fine (;, but I've got a problem with rand() in 
awk.  It seems to be initialized with the unix time to the second, and 
gives completely predicatable results.

Running

BEGIN {
	srand();	# seed to the Unix time
	print int(rand() * 10);
}

every second, just marches up from 0 to 9.  Now, my problem is that I 
want to run this script from a cron job every say X seconds, which means 
that unless I make sure that my quotes file's length doesn't have any 
divisors in common, I'll be getting the same results.

So, question is:

Can I seed using srand() and something like /dev/urandom, or is there an 
altogther simpler way?

Thanks for any ideas you could throw my way,

James.

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list