🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
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.
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 )
You might have to scale the number if you start at 1.
int GetRandomInteger( void )
{
int x; // random integer
x = ( rand() % 100 ) + 1; // generates a random number
// between 0-99, then adds 1 (1-100)
return x;
}
int GetRandomInteger( void )
{
int x; // random integer
x = ( rand() % 100 ) + 1; // generates a random number
// between 0-99, then adds 1 (1-100)
return x;
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement