summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Friis <peavey@placid.dk>2008-11-03 20:52:44 +0000
committerDennis Friis <peavey@placid.dk>2008-11-03 20:52:44 +0000
commitbb58ba1518b3ee4dd0615984b689f827acfc32de (patch)
tree3768917bb7d3d4b1e17820ad1b1a4518898ef346
parentf7d5517952b41361b9184cf4a32425f93f703936 (diff)
downloadtmwa-bb58ba1518b3ee4dd0615984b689f827acfc32de.tar.gz
tmwa-bb58ba1518b3ee4dd0615984b689f827acfc32de.tar.bz2
tmwa-bb58ba1518b3ee4dd0615984b689f827acfc32de.tar.xz
tmwa-bb58ba1518b3ee4dd0615984b689f827acfc32de.zip
Rename MMRAND to MPRAND and clarify in a comment how it works exactly. These are purpose-specific replacements for the vast usage of rand() with modulo and using high-order bits instead.
-rw-r--r--src/common/utils.h11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/common/utils.h b/src/common/utils.h
index fa5cf57..794a0b6 100644
--- a/src/common/utils.h
+++ b/src/common/utils.h
@@ -31,5 +31,12 @@
if (!((result) = (type *) realloc ((result), sizeof(type) * (number))))\
{ printf("SYSERR: realloc failure"); abort(); } } while(0)
-#define MRAND(max) (int) ((float)max * (rand() / (RAND_MAX + 1.0)))
-#define MMRAND(min, max) min + (int) ((float)max * (rand() / (RAND_MAX + (float)min)))
+/*
+ * ModuloRand and ModuloPlusRand. These macros are used to replace the
+ * vast number of calls to rand()%mod which is low-order bits. These
+ * instead use the high-order bits as suggested in the man page of rand().
+ * MRAND(10), returns 0-9.
+ * MPRAND(5,10) returns 5-14.
+ */
+#define MRAND(mod) (int) ((float)mod * (rand() / (RAND_MAX + 1.0)))
+#define MPRAND(add, mod) add + MRAND(mod)