Jump to content
  • Advertisement

Archived

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

Metal Typhoon

Stdlib.h

This topic is 6224 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

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 this post


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


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
This should work I did last one in c++ and
forgot the srand.

  
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!