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

James Goldwater james at eccehomo.co.uk
Wed Jun 30 16:24:05 UTC 2004


Wow, I was using mawk 1.3.3 - this is on debian woody.  Never realised 
that! I've just apt-get'd gawk and you're dead right: using gawk 3.1.0 
fixes this behaviour.

Thanks very much for the help!

James.


Nix wrote:
> On Wed, 30 Jun 2004, James Goldwater uttered the following:
> 
>>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.
> 
> 
> I can't replicate this with GNU awk 3.1.3.
> 
> Is this GNU awk?
> 
> If so, rand() changed implementations in gawk 3.1.0; I'm not sure what
> implementation it was using before that date, but as of 3.1.0+, the
> random() function is akin to the one in glibc (ultimately taken from the
> same source in BSD, although the glibc one has changed somewhat since),
> using incerasingly strong random number generators as the size of the
> state vector rises --- and awk gives it 512 bytes of state. The
> resulting RNG should have a huge cycle time and never emit such horribly
> nonrandom numbers.
> 
> The nonrandom lower bits is symptomatic of a poor linear congruential
> generator, as awk 3.1.x uses when the state provided is tiny (which it
> never is in that version of awk).
> 
> Perhaps your version of awk supports nothing but such a linear
> congruential generator?
> 


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




More information about the GLLUG mailing list