• Advertisement

Archived

This topic is now archived and is closed to further replies.

A stupid question

This topic is 5935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
randomnr=rand()%100 ; // random number between 0->100

you also need the following line included :

srand((unsigned)time(NULL)) ;

and include time.h

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

  • Advertisement