vendredi 19 avril 2019

circular #defines in C

Please note -- I didn't write this code, I'm just trying to maintain / improve it.

One of my header files contains these lines:

/* macros to seed the random number generator */
/* This is needed on Windows which throws an error due to 'random' 
not being defined on MingW.  I'll clean it up later. */
#define srandom srand
#define random rand

/* New random seed function. */
#define srand srandom
#define rand random

#define rnd(x)  ((int)(rand() % (x)) + 1)
#define rund(x) ((int)(rand() % (x)))

Furthermore, I have man pages for all four functions rand, srand, random, srandom, so I would assume that these are all valid C functions.

Can you help me understand / figure out what happens when I call rnd(10) ? Is this undefined behavior? Does one definition "override" the other somehow?

(I'm asking because I feel like I get a lot of low numbers in my actual program, even though my test programs seem to work correctly and distribute randomly.)




Aucun commentaire:

Enregistrer un commentaire