[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Apparent issue with srand() in gawk
From: |
Jim Mellander |
Subject: |
Apparent issue with srand() in gawk |
Date: |
Wed, 11 Jun 2025 00:26:08 -0700 |
Hi all:
I run several instances of an awk script per second, and need distinct random
numbers per run. The standard srand() seeds from the time of day with a 1
second granularity, and so is not usable in this context.
What I'm doing is:
cmd = "od -t u4 -N 4 /dev/urandom"
cmd | getline
close(cmd)
srand($2)
which pulls a 4 byte value from /dev/urandom & uses it as a seed.
What astonishes me is that gawk (unlike mawk, and other awks that I've tested)
generates the same initial result from rand() for multiple seeds, e.g. :
Seed rand()
2542480914 0.901255
2248009812 0.901255
3603781759 0.901255
3192953181 0.901255
3694300546 0.901255
3741295576 0.901255
3361282495 0.901255
3556126043 0.901255
3103474738 0.901255
3367665948 0.901255
2595174406 0.901255
3809429360 0.901255
2898584410 0.901255
3896527708 0.901255
4034701533 0.901255
3490410576 0.901255
3233481511 0.901255
4055551773 0.901255
2513801703 0.901255
3660630961 0.901255
I assume that this is related to the builtin rand() implementation. This is on
a 32-bit Archlinux ARM installation. doesn't happen on my 64-bit M1 Mac. A few
factors that seem relevant to me:
32-bit
ARM, no hardware FP
compiler GCC 14.2.0
gawk 5.3.2
Interestingly, if I pull less than 4 bytes from /dev/urandom for a seed (e.g.
"od -t u4 -N 3 /dev/urandom") random numbers are generated as expected. Note
that all the seeds in the examples above are > 2^31, which could indicate a
signed vs unsigned int issue.
Jim
- Apparent issue with srand() in gawk,
Jim Mellander <=