diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2016-12-18 18:00:45 +0100 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2016-12-18 18:00:45 +0100 |
commit | 4f72cda4acaa92dfaddb29891d86efeea990e030 (patch) | |
tree | 7ab5e3aa3dde5f78012a372c7809f6fe26adf312 /sys/src/cmd/awk | |
parent | 2830cd7eb6182eb7bc42e10557a160156649859c (diff) |
awk: improve random number generation
don't use rand() and scale it to 0..1, instead call
native frand() which produces uniform random number.
instead of seeding the rng with time(0), use truerand().
Diffstat (limited to 'sys/src/cmd/awk')
-rw-r--r-- | sys/src/cmd/awk/run.c | 9 |
1 files changed, 2 insertions, 7 deletions
diff --git a/sys/src/cmd/awk/run.c b/sys/src/cmd/awk/run.c index 6627f661c..213fb25ba 100644 --- a/sys/src/cmd/awk/run.c +++ b/sys/src/cmd/awk/run.c @@ -29,10 +29,6 @@ THIS SOFTWARE. #include "awk.h" #include "y.tab.h" -#ifndef RAND_MAX -#define RAND_MAX 32767 /* all that ansi guarantees */ -#endif - jmp_buf env; extern int pairstack[]; @@ -1582,12 +1578,11 @@ Cell *bltin(Node **a, int) /* builtin functions. a[0] is type, a[1] is arg list u = (Awkfloat) system(getsval(x)); break; case FRAND: - /* in principle, rand() returns something in 0..RAND_MAX */ - u = (Awkfloat) (rand() % RAND_MAX) / RAND_MAX; + u = frand(); break; case FSRAND: if (isrec(x)) /* no argument provided */ - u = time(nil); + u = (Awkfloat) (truerand() >> 1); else u = getfval(x); srand((unsigned int) u); |