diff options
author | pfg <pfg@FreeBSD.org> | 2014-10-10 23:18:44 +0000 |
---|---|---|
committer | pfg <pfg@FreeBSD.org> | 2014-10-10 23:18:44 +0000 |
commit | e384592b57c8db7e176d49ea18582f5ebc6c365b (patch) | |
tree | dd2d486a0bc2a63d26e4050eb60d759e25882cd2 /contrib/one-true-awk/run.c | |
parent | 5595ee40038c5126db4f066c82af2fb2883d2946 (diff) | |
download | FreeBSD-src-e384592b57c8db7e176d49ea18582f5ebc6c365b.zip FreeBSD-src-e384592b57c8db7e176d49ea18582f5ebc6c365b.tar.gz |
MFC r271879:
awk: Use random(3) instead of rand(3)
While none of them is considered even near to cryptographic
level, random(3) is a better random generator than rand(3).
Use random(3) for awk as is done in other systems.
PR: 193147
Diffstat (limited to 'contrib/one-true-awk/run.c')
-rw-r--r-- | contrib/one-true-awk/run.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/contrib/one-true-awk/run.c b/contrib/one-true-awk/run.c index cafaff3..2bcd9df 100644 --- a/contrib/one-true-awk/run.c +++ b/contrib/one-true-awk/run.c @@ -1521,8 +1521,10 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis u = (Awkfloat) system(getsval(x)) / 256; /* 256 is unix-dep */ break; case FRAND: - /* in principle, rand() returns something in 0..RAND_MAX */ - u = (Awkfloat) (rand() % RAND_MAX) / RAND_MAX; + /* random() returns numbers in [0..2^31-1] + * in order to get a number in [0, 1), divide it by 2^31 + */ + u = (Awkfloat) random() / (0x7fffffffL + 0x1UL); break; case FSRAND: if (isrec(x)) /* no argument provided */ @@ -1530,7 +1532,7 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis else u = getfval(x); tmp = u; - srand((unsigned int) u); + srandom((unsigned long) u); u = srand_seed; srand_seed = tmp; break; |