#include <iostream>
#include <time.h>
using namespace std;
int main()
{
//Seed the random generator
srand( (unsigned)time( NULL ) );
int number = rand() % 100;
int guess;
cout << "I am thinking of a number between 1 and 100" << endl;
cout << "Enter your guess, please ";
cin >> guess;
if (guess == number)
{
cout << "Incredible, you are correct" << endl;
}
else
{
cout << "HA HA, YOU SUCK!!!" << endl;
}
return 0;
}
After i finish with this random thing i gonna make a "loop
Last edited by Don Pwnious on Sat Sep 04, 2004 2:17 pm, edited 2 times in total.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//Function: RandInt()
//Parameters: min, max
//Description: Returns a random int between a min and max
int RandInt(int min, int max)
{
//Seed the random generator
srand( (unsigned)time( NULL ) );
//Generate and Return an interger between min and max
return (rand() % max) + min;
}
//Nothing imaportant
char Blaa;
int main(int argc, char *argv[])
{
//Display a random number
cout << "Woot Woot, a random number: " << RandInt(0,100);
//Keeps the console box from closing to soon
cin >> Blaa;
return 0;
}
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Except the best way to guarantee a random number is to seed with your last random number. :)
And you don't have a loop here :P
AHA! Okay, I got you. No, you don't need to reseed unless you loop.
You would do something like this, I suppose -
and what sucks is that i can't format text inside code brackets. i'll have to see about changing that somewhere... sometime.
int main()
{
//Seed the random generator with time
srand( time( NULL ) );
int number;
int rawNumber = rand(); //generate a random number
int guess;
while (/*some condition*/) {
//these lines here generate a new number every time the loop
//goes through an iteration. (ooh big word look out)
srand(rawNumber); //this seeds with your last random number
rawNumber = rand();
number = rawNumber % 100;
cout << "I am thinking of a number between 1 and 100" << endl;
cout << "Enter your guess, please ";
cin >> guess;
if (guess == number)
{
cout << "Incredible, you are correct" << endl;
break; //leave the loop
}
else
{
cout << "HA HA, YOU SUCK!!!" << endl
<< "NEW NUMBER!!! BWAAHAHAHAHA" << endl;
}
} //close the while loop
return 0;
}
Can rand() return values < 0? :P
Last edited by MarauderIIC on Sat Sep 04, 2004 2:49 pm, edited 1 time in total.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Looked it up. Between zero and RAND_MAX, which is a predefined constant. In MSVC++ libraries, it's 0x7fff which mr. calculator says is 32,767 which I believe but I keep coming up 5,000 short. :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//Function: RandInt()
//Parameters: min, max
//Description: Returns a random int between a min and max
int RandInt(int min, int max)
{
//Seed the random generator with the last random number
srand( rand() );
//Generate and Return an interger between min and max
return (rand() % (max-min+1)) + min;
}
//Nothing imaportant
char Blaa;
int main(int argc, char *argv[])
{
//Seed the random generator with time (only time you have to do so)
srand( time( NULL ) );
//Display a random number
cout << "Woot Woot, a random number: " << RandInt(10,20);
//Keeps the console box from closing to soon
cin >> Blaa;
return 0;
}
Small girl at the harbor wrote:Look Brandon, that crab's got ham!