diff options
author | Pedro F. Giffuni <pfg@FreeBSD.org> | 2014-09-19 18:24:02 +0000 |
---|---|---|
committer | Pedro F. Giffuni <pfg@FreeBSD.org> | 2014-09-19 18:24:02 +0000 |
commit | a4b2ac79e43381fa4f3b9be9e4f8ac1b8a0fac17 (patch) | |
tree | 9f75d6e8b889afe0d40cdbced87f33cf8ca7279c /contrib/one-true-awk | |
parent | 749cd43178daf07374238a0ee37ede58341b431c (diff) | |
download | src-test2-a4b2ac79e43381fa4f3b9be9e4f8ac1b8a0fac17.tar.gz src-test2-a4b2ac79e43381fa4f3b9be9e4f8ac1b8a0fac17.zip |
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.
Thanks to Chenguang Li for discussing this in the lists
and submitting the patch upstream.
PR: 193147
MFC after: 5 weeks
Notes
Notes:
svn path=/head/; revision=271879
Diffstat (limited to 'contrib/one-true-awk')
-rw-r--r-- | contrib/one-true-awk/awk.1 | 2 | ||||
-rw-r--r-- | contrib/one-true-awk/main.c | 2 | ||||
-rw-r--r-- | contrib/one-true-awk/run.c | 8 |
3 files changed, 7 insertions, 5 deletions
diff --git a/contrib/one-true-awk/awk.1 b/contrib/one-true-awk/awk.1 index 6119613c1aae..b0d243b495cc 100644 --- a/contrib/one-true-awk/awk.1 +++ b/contrib/one-true-awk/awk.1 @@ -208,7 +208,7 @@ or of if no argument. .TP .B rand -random number on (0,1) +random number on [0,1) .TP .B srand sets seed for diff --git a/contrib/one-true-awk/main.c b/contrib/one-true-awk/main.c index ab01676f394e..ec7029454386 100644 --- a/contrib/one-true-awk/main.c +++ b/contrib/one-true-awk/main.c @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) signal(SIGFPE, fpecatch); srand_seed = 1; - srand(srand_seed); + srandom((unsigned long) srand_seed); yyin = NULL; symtab = makesymtab(NSYMTAB/NSYMTAB); diff --git a/contrib/one-true-awk/run.c b/contrib/one-true-awk/run.c index cafaff3e4ad9..2bcd9dfa252a 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; |