Public Group

#### Archived

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

# A stupid question

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

## 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 on other sites
randomnr=rand()%100 ; // random number between 0->100

you also need the following line included :

srand((unsigned)time(NULL)) ;

and include time.h

##### 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 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;
}

1. 1
Rutin
54
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633412
• Total Posts
3011734
• ### Who's Online (See full list)

There are no registered users currently online

×