ref: bcc5530dab33d4603c421ae31be64d2ff3be7ec5
parent: 01a3564915372e4c5a57429fb56fd5dfbbf88ec3
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Dec 18 13:00:45 EST 2016
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().
--- a/sys/src/cmd/awk/run.c
+++ b/sys/src/cmd/awk/run.c
@@ -29,10 +29,6 @@
#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 @@
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);
--
⑨