A stupid question
Ok this is something I would look up but I lost all of my
c books in the madness that has been my life as of late.
I just need to know what function I can use to return
a random number between 0-X. I though it was randomize(limit);
but I cant seam to find where it is prototyped. Its been awhile
since I used it and I just need it to fill my tile map with
random tiles while I finnish building the engine.
------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
randomnr=rand()%100 ; // random number between 0->100
you also need the following line included :
srand((unsigned)time(NULL)) ;
and include time.h
you also need the following line included :
srand((unsigned)time(NULL)) ;
and include time.h
Be careful that the low-order bit of rand() are poorly random (if you take my meaning).
You actually want to do
x = 1 + (int)( 100.0 * rand() / ( 1.0 + RAND_MAX ) );
which will use the high order bits. Of course, it is slower, but it is more random.
cf Numerical Recipes, chapter 7, p277. (online at http://lib-www.lanl.gov/numerical/bookcpdf/c7-1.pdf )
You actually want to do
x = 1 + (int)( 100.0 * rand() / ( 1.0 + RAND_MAX ) );
which will use the high order bits. Of course, it is slower, but it is more random.
cf Numerical Recipes, chapter 7, p277. (online at http://lib-www.lanl.gov/numerical/bookcpdf/c7-1.pdf )
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement