From 344c5d3c82baae8a5f13cb40d0d5ef383fb5692f Mon Sep 17 00:00:00 2001 From: diligentcircle Date: Fri, 20 Aug 2021 17:48:18 -0400 Subject: [PATCH] Improved the CHANCE implementation a bit. This makes the implementation more uniform (avoiding skew caused by %). --- src/defs.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/defs.h b/src/defs.h index 171609a..c1406ac 100644 --- a/src/defs.h +++ b/src/defs.h @@ -29,8 +29,9 @@ along with this program. If not, see . #define WRAP_ADD(x, y, a, b) x = (((x) + (y)) + \ ((x) + (y) < (a) ? ((b) - (a)) : 0) + \ ((x) + (y) > (b) ? ((a) - (b)) : 0)) -#define CHANCE(x) ((rand() % RAND_MAX) < ((x) * RAND_MAX)) -#define RANDRANGE(x, y) (((x) < (y)) ? ((x) + (rand() % (long)(1 + (y) - (x)))) : (x)) +#define CHANCE(x) (((double)rand() / ((double)RAND_MAX+1)) < (x)) +#define RANDRANGE(x, y) (((x) < (y)) ? \ + ((x) + (rand() % (long)(1 + (y) - (x)))) : (x)) #define DRAND ((double)rand() / RAND_MAX) #define _(s) gettext(s) #define CSDLP(x) (((x) == SDL_PRESSED) ? 1 : 0)