Need a random number generator in my C++ guess the number game

Hi
I want to throw a random number generator into my program here. I've tried a few ways from other codes online but cant them to work. I've tried some things the webcite ut, can't get them to work with my program. Any suggestions?





#include <iostream>
using namespace std;


int main() {
int n, x;
cout <<"This is a guess the number game." <<endl;
cout <<"This game is also my first progam that I've written on my own." <<endl;
cout <<"It's so amateur that you can play it only once." <<endl;
cout <<"If you try to play it again you'll just be guessin' the same number." <<endl;
cout <<"start guessin':";

n = 13;
cin >> x;
while (x < n){
cout <<"Higher!Try again:";
cin >> x;

}while (x > n){
cout <<"Lower!Try again:";
cin >> x;

}if (x == n) {
cout <<"Bingo!!!";
}



cout<< endl;

return 0;

}
Hello.

There is an example in this page. Pls check this. http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

Thanks
Mahesh
MyBlog: http://login2win.blogspot.com/
I would suggest using the Boost library. It has a variety of random number algos you can use.
you could also use
1
2
srand((unsigned)time(NULL));
n=rand();


you could also use a modulo to limit the number your guessing
1
2
n %= 10;
n++;


that will get you numbers 1-10. this is all in time.h
Topic archived. No new replies allowed.