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

git-svn-id: svn+ssh://svn.freebsd.org/base/head@271879 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f
This commit is contained in:
pfg 2014-09-19 18:24:02 +00:00 committed by Arnold D. Robbins
parent 650d868ec4
commit c70b9fe882
3 changed files with 7 additions and 5 deletions

2
awk.1
View File

@ -209,7 +209,7 @@ or length of
if no argument.
.TP
.B rand
random number on (0,1).
random number on [0,1).
.TP
.B srand
sets seed for

2
main.c
View File

@ -78,7 +78,7 @@ int main(int argc, char *argv[])
/*signal(SIGSEGV, segvcatch); experiment */
srand_seed = 1;
srand(srand_seed);
srandom((unsigned long) srand_seed);
yyin = NULL;
symtab = makesymtab(NSYMTAB/NSYMTAB);

8
run.c
View File

@ -1579,8 +1579,10 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis
}
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 */
@ -1588,7 +1590,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;