[Gllug] Obscure question with rand() in awk, maybe a better way
Nix
nix at esperi.org.uk
Wed Jun 30 15:45:27 UTC 2004
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?
--
`Some people find it difficult to accept that it is not always possible
to explain things which should be explicable.'
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list