• ### Popular Now

• 11
• 14
• 19
• 27
• 9

#### Archived

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

# Stdlib.h

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

## Recommended Posts

I''m using Vc++ and i included the stdlib.h . But i''m trying to generate a random number but it does. I do this: short Test; Test = random(10); cout <<"Test"< How can i generate a random number in vc++ ? Thank''s "The shortcut is not always the best way " Metal Typhoon

##### Share on other sites
It''s rand(), not random().

~~~~~~~~~~
Martee

##### Share on other sites
i''ve tried that , look what it turn back to me

C:\Metal Softwares\Generator\Generator.cpp(23) : error C2660: ''rand'' : function does not take 1 parameters

and i did

rand(3);

Thanks

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
Sorry ... my mistake ...
rand() takes no parameters. It returns a number between 0 and RAND_MAX. So to generate a random number between 0 and 9, you would use 'rand() % 10'.

Edit: You'll also want to include time.h, and do a 'srand((unsigned int) time(0));'at the beginning of your program.

~~~~~~~~~~
Martee

Edited by - Martee on June 28, 2001 11:10:43 PM

##### Share on other sites

"C:\Metal Softwares\Generator\Generator.cpp(23) : error C2660: ''rand'' : function does not take 1 parameters"

And look into the help files more often.
This means no parameter...
So, it should be ...

Test = rand ( ); // rand ( empty )

Here is an example from the help files...

#include
#include
#include

void main( void )
{
int i;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
// srand ( time ) to make it random based on time...
// or else u get the same number twice.

/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}

"But why take the long way when the shortest is faster?"

BrownBean

##### Share on other sites
So if i just want to generate a random number between 0 2 i do this:

rand() % 3;

???

"The shortcut is not always the best way "

Metal Typhoon

Yes.

~~~~~~~~~~
Martee

##### Share on other sites
I have a vc++ version that i got on the internet so, i think that i can''t use the help.

Anyways i''ll buy the standard version, does that come with help stuff ?

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
this doesn''t work

  #include #include int main(){ // generate random number between 0 and 9 std::cout << std::rand() % 10 << std::endl; return 0;}

  #include #include #include int main(void){ srand(time(NULL)); printf("rand 0-9: %d\n", rand() % 10); return 0;}