Stdlib.h
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
Metal Typhoon
It''s rand(), not random().
~~~~~~~~~~
Martee
~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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
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
Metal Typhoon
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
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
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
June 28, 2001 10:18 PM
You should read carefully.
"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
"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
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
rand() % 3;
???
"The shortcut is not always the best way "
Metal Typhoon
Metal Typhoon
Yes.
~~~~~~~~~~
Martee
~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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
Anyways i''ll buy the standard version, does that come with help stuff ?
"The shortcut is not always the best way "
Metal Typhoon
Metal Typhoon
this doesn''t work
#include <iostream>#include <cstdlib>int main(){ // generate random number between 0 and 9 std::cout << std::rand() % 10 << std::endl; return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement