From bb58ba1518b3ee4dd0615984b689f827acfc32de Mon Sep 17 00:00:00 2001 From: Dennis Friis Date: Mon, 3 Nov 2008 20:52:44 +0000 Subject: 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. --- src/common/utils.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') 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) -- cgit v1.2.3-70-g09d2