Noob question for random number

• 01-03-2003
SKINp
Noob question for random number
Ok, I'm new to c++, I'm trying to make my second program. And I would like to know what is the c++ operation to get a random number. Can you help me??

And as we are there, can you give it to me in a usefull syntax??

Like can you tell me how to get:

x = ( random number between 1 and 10 )
• 01-03-2003
alkis_y3k
This might not be the precise syntax, but:
Code:

int h = rand() % 100;
This will generate a 'fake' random number from 1 to 100 i think. However, I could be mistaken. Anyone?
• 01-03-2003
Prelude
> x = ( random number between 1 and 10 )
int x = (int)( (double)rand() * 10 / RAND_MAX + 1 );

>This will generate a 'fake' random number from 1 to 100 i think.
From 0 to 99.

>int h = rand() % 100;
This is a bad way to get a random number for reasons which I've stated many times before, do a board search for more details.

-Prelude
• 01-03-2003
joshdick
Quote:

Originally posted by alkis_y3k
This might not be the precise syntax, but:
Code:

int h = rand() % 100;
This will generate a 'fake' random number from 1 to 100 i think. However, I could be mistaken. Anyone?

You're a little off, there. The code u gave will generate a random number from 0 to 99.

SKINp, do a search of this board for random numbers. You should be able to find tons of threads on generating random numbers within certain bounds and using your computer's internal clock to make the pseudo-random numbers seem more random.
• 01-03-2003
Munkey01
If you would like true random numbers and have alot of money, I bet the people at LavaRand would be glad to make you a setup.
• 01-03-2003
SKINp
Hee in each of your way, I get an error telling me:

implicit declaration of fonction 'int rand(...)'
• 01-03-2003
Munkey01
Make sure to include stdlib.h. ;)