diff options
author | ache <ache@FreeBSD.org> | 2008-08-08 01:02:30 +0000 |
---|---|---|
committer | ache <ache@FreeBSD.org> | 2008-08-08 01:02:30 +0000 |
commit | c14ceb129d096f937348a276508fee6f9493f9ae (patch) | |
tree | 524618d341fe68d9181fc0ed65fb8208868a15af /games/random | |
parent | 9bb760c8093c23616ef4fbe22601d34cc222fab6 (diff) | |
download | FreeBSD-src-c14ceb129d096f937348a276508fee6f9493f9ae.zip FreeBSD-src-c14ceb129d096f937348a276508fee6f9493f9ae.tar.gz |
1) Replace hardcoded RANDOM_MAX macro with wrong (1 less than must be)
value with ((double)RAND_MAX + 1)
2) For exit code increase valid denominator upper range from 255 to 256
since returned value is [0 .. denom - 1]
Diffstat (limited to 'games/random')
-rw-r--r-- | games/random/random.c | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/games/random/random.c b/games/random/random.c index b7b732a..601036f 100644 --- a/games/random/random.c +++ b/games/random/random.c @@ -63,12 +63,6 @@ __FBSDID("$FreeBSD$"); #include "randomize_fd.h" -/* - * The random() function is defined to return values between 0 and - * 2^31 - 1 inclusive in random(3). - */ -#define RANDOM_MAX 0x7fffffffL - static void usage(void); int @@ -137,8 +131,8 @@ main(int argc, char *argv[]) err(1, "%s", *argv); if (denom <= 0 || *ep != '\0') errx(1, "denominator is not valid."); - if (random_exit && denom > 255) - errx(1, "denominator must be <= 255 for random exit."); + if (random_exit && denom > 256) + errx(1, "denominator must be <= 256 for random exit."); break; default: usage(); @@ -168,7 +162,7 @@ main(int argc, char *argv[]) /* Compute a random exit status between 0 and denom - 1. */ if (random_exit) - return (int)((denom * random()) / RANDOM_MAX); + return (int)(denom * random() / ((double)RAND_MAX + 1)); /* * Select whether to print the first line. (Prime the pump.) @@ -176,7 +170,7 @@ main(int argc, char *argv[]) * 0 (which has a 1 / denom chance of being true), we select the * line. */ - selected = (int)(denom * random() / RANDOM_MAX) == 0; + selected = (int)(denom * random() / ((double)RAND_MAX + 1)) == 0; while ((ch = getchar()) != EOF) { if (selected) (void)putchar(ch); @@ -186,7 +180,7 @@ main(int argc, char *argv[]) err(2, "stdout"); /* Now see if the next line is to be printed. */ - selected = (int)(denom * random() / RANDOM_MAX) == 0; + selected = (int)(denom * random() / ((double)RAND_MAX + 1)) == 0; } } if (ferror(stdin)) |