PDA

Click to See Complete Forum and Search --> : Random


Khavoerm
Oct 20th, 2001, 08:32 AM
Hello, how can I get a number between 1 and 6 in C++?

Vlatko
Oct 20th, 2001, 09:36 AM
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char* argv[])
{
srand((unsigned)time( NULL ));
cout<<rand() % 7<<endl;
return 0;
}

Vlatko
Oct 20th, 2001, 09:37 AM
Oh, this will generate 0 too. You may use if..else to check if it is 0 use rand again.

transcendental
Oct 20th, 2001, 10:02 AM
Originally posted by Vlatko

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char* argv[])
{
srand((unsigned)time( NULL ));
cout<<(rand() % 6)+1<<endl;
return 0;
}


How about that?

Vlatko
Oct 20th, 2001, 12:28 PM
LOL :D

When you are in a hurry even that happens.
:D

transcendental
Oct 20th, 2001, 03:45 PM
Originally posted by Vlatko
LOL :D

When you are in a hurry even that happens.
:D

It happens to me all the time.

Khavoerm
Oct 20th, 2001, 07:04 PM
Thanks for the help