I am wondering if there is some code that lets you creat a number one through ten randomly? any help here would be appreciated
just a simple guessing game
Started by deryni, Dec 12 2008 04:44 AM
2 replies to this topic
#1
Posted 12 December 2008 - 04:44 AM
|
|
|
#2
Posted 12 December 2008 - 05:48 AM
I'm assuming your using C++.
In your includes make sure you have:
And use this function, it will produce a random number between 1 and 10 when called:
In your includes make sure you have:
#include <cstdlib> #include <ctime>
And use this function, it will produce a random number between 1 and 10 when called:
int randfunction()
{
std::srand(time(0));
return std::rand() % 10 + 1;
}
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
#3
Posted 13 December 2008 - 03:57 PM
#include <iostream>
#include <ctime>
using namespace std;
int getRandomNumber(int min, int max)
{
int range = (max - min) + 1;
int number = min + (int)(range * rand() / (RAND_MAX + 1.0));
return(number);
}
int main(void)
{
srand(time(0));
int answer = getRandomNumber(1,10);
int input = 0;
cout<<"I am thinking of a number between 1 and 10. Can you guess it?"<<endl;
while(input != answer) {
cout<<"Guess: ";
cin>>input;
if(input == answer) {
break;
}
cout<<"Wrong! Try again."<<endl;
}
cout<<"Correct! "<<answer<<" was the number I thought of."<<endl;
cin.get();
}


Sign In
Create Account

Back to top









