my program includes function uses rand()
. function called more once can't put srand()
@ beginning of function. now, there general rule suggests whether srand()
should put in header file of function, or in beginning of main function?
my understanding both work same. difference if want reuse function in future, having srand()
in header makes function more self-contained, may end multiple headers having srand()
.
--edit--
yes, mean like
/*header.h*/ srand(); void my_funciton();
so won't work?
now, there general rule suggests whether srand() should put in header file of function
if mean like
/** * foo.h */ srand(); void my_function_that_uses_rand();
that won't cause srand
called whatever includes header file; treated old-style implicit int
function declaration (which not work c99 or later compilers, since implicit int
function declarations no longer supported), cause other problems if you've included math.h
in whatever file including file.
edit
actually, it's worse that, because call srand
requires seed parameter, if wanted randomize on time, header file like
/** * foo.h */ srand(time(null)); void my_function_that_uses_rand();
which compiler yak on, since srand(time(null));
not valid declaration in first place.
srand
should called once before first call rand
, , it's application code so. note if call srand
same seed value, same sequence of values in rand
calls.
Comments
Post a Comment