Browse Source

Simplify return call of rand() and rand_r() to make it easier to read.

This is slightly less robust, but RAND_MAX must be one below a power
of two in both variants anyway.
Based on a suggestion by Matthew Martin.
ok tedu@
OPENBSD_5_9
tb 9 years ago
parent
commit
90b17882f1
1 changed files with 2 additions and 2 deletions
  1. +2
    -2
      src/lib/libc/stdlib/rand.c

+ 2
- 2
src/lib/libc/stdlib/rand.c View File

@ -37,7 +37,7 @@ int
rand_r(u_int *seed) rand_r(u_int *seed)
{ {
*seed = *seed * 1103515245 + 12345; *seed = *seed * 1103515245 + 12345;
return (*seed % ((u_int)RAND_MAX + 1));
return (*seed & RAND_MAX);
} }
DEF_WEAK(rand_r); DEF_WEAK(rand_r);
@ -50,7 +50,7 @@ int
rand(void) rand(void)
{ {
if (rand_deterministic == 0) if (rand_deterministic == 0)
return (arc4random() % ((u_int)RAND_MAX + 1));
return (arc4random() & RAND_MAX);
return (rand_r(&next)); return (rand_r(&next));
} }


Loading…
Cancel
Save