summaryrefslogtreecommitdiffstats
path: root/games
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2008-08-08 01:42:17 +0000
committerache <ache@FreeBSD.org>2008-08-08 01:42:17 +0000
commit823689b09c9db6b0a03a78837e4c02b453e37d8d (patch)
tree8f6ee5f63b889dc3ddd277538eb981661d977ec6 /games
parentc14ceb129d096f937348a276508fee6f9493f9ae (diff)
downloadFreeBSD-src-823689b09c9db6b0a03a78837e4c02b453e37d8d.zip
FreeBSD-src-823689b09c9db6b0a03a78837e4c02b453e37d8d.tar.gz
I was confused a bit by the wrong construction with RAND_MAX
used in randomize_fd.c. Although the max value is the same currently, RAND_MAX is for rand(), not for random(). So move RANDOM_MAX const to the common file now, make it UL and use in randomize_fd.c too. (in any case its old value was 1 less then must be, as noted in the prev. commit)
Diffstat (limited to 'games')
-rw-r--r--games/random/random.c6
-rw-r--r--games/random/randomize_fd.c2
-rw-r--r--games/random/randomize_fd.h6
3 files changed, 10 insertions, 4 deletions
diff --git a/games/random/random.c b/games/random/random.c
index 601036f..122ce45 100644
--- a/games/random/random.c
+++ b/games/random/random.c
@@ -162,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() / ((double)RAND_MAX + 1));
+ return (int)(denom * random() / RANDOM_MAX);
/*
* Select whether to print the first line. (Prime the pump.)
@@ -170,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() / ((double)RAND_MAX + 1)) == 0;
+ selected = (int)(denom * random() / RANDOM_MAX) == 0;
while ((ch = getchar()) != EOF) {
if (selected)
(void)putchar(ch);
@@ -180,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() / ((double)RAND_MAX + 1)) == 0;
+ selected = (int)(denom * random() / RANDOM_MAX) == 0;
}
}
if (ferror(stdin))
diff --git a/games/random/randomize_fd.c b/games/random/randomize_fd.c
index b60c53c..e1011fb 100644
--- a/games/random/randomize_fd.c
+++ b/games/random/randomize_fd.c
@@ -202,7 +202,7 @@ randomize_fd(int fd, int type, int unique, double denom)
}
for (i = numnode; i > 0; i--) {
- selected = ((int)denom * random())/(((double)RAND_MAX + 1) / numnode);
+ selected = ((int)denom * random())/(RANDOM_MAX / numnode);
for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
if (j == selected) {
diff --git a/games/random/randomize_fd.h b/games/random/randomize_fd.h
index 5b50e62..6f9fac2 100644
--- a/games/random/randomize_fd.h
+++ b/games/random/randomize_fd.h
@@ -29,6 +29,12 @@
#ifndef __RANDOMIZE_FD__
#define __RANDOMIZE_FD__
+/*
+ * The random() function is defined to return values between 0 and
+ * 2^31 - 1 inclusive in random(3).
+ */
+#define RANDOM_MAX 0x80000000UL
+
#define RANDOM_TYPE_UNSET 0
#define RANDOM_TYPE_LINES 1
#define RANDOM_TYPE_WORDS 2
OpenPOWER on IntegriCloud