summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2011-09-30 23:53:23 -0700
committerBen Longbons <b.r.longbons@gmail.com>2011-10-01 00:27:37 -0700
commitd5cdae1592522e330bac0e197a449d8328c1d5dc (patch)
tree9094e556d00665b564687c37609b987cc23744ba
parent840420f4014e03318d4258f934110ddb11c11942 (diff)
downloadtmwa-d5cdae1592522e330bac0e197a449d8328c1d5dc.tar.gz
tmwa-d5cdae1592522e330bac0e197a449d8328c1d5dc.tar.bz2
tmwa-d5cdae1592522e330bac0e197a449d8328c1d5dc.tar.xz
tmwa-d5cdae1592522e330bac0e197a449d8328c1d5dc.zip
Fix negative chances wrapping to high (Another bug in the language)
-rw-r--r--src/common/mt_rand.h5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/common/mt_rand.h b/src/common/mt_rand.h
index 95f30e5..84d32e5 100644
--- a/src/common/mt_rand.h
+++ b/src/common/mt_rand.h
@@ -15,7 +15,10 @@ uint32_t mt_random (void);
* MRAND(10) returns 0..9
* MPRAND(5,10) returns 5..14
*/
-# define MRAND(mod) (mt_random() % (mod))
+// The cast is essential because the result is sometimes
+// compared with a possibly negative number.
+// Because it's using modulus, high numbers shouldn't happen anyway.
+# define MRAND(mod) ((int)(mt_random() % (mod)))
# define MPRAND(add, mod) ((add) + MRAND(mod))
#endif // MT_RAND_H