I am creating a simple program which is in need of a simple random number generator. How would I make it so that it chooses a random number between 1 and 10?
I am creating a simple program which is in need of a simple random number generator. How would I make it so that it chooses a random number between 1 and 10?
rand() gives a random integer between 0 and RAND_MAX. Use modulus (a % b) on the result to cut it into your desired interval.
Or as a more general function:Code:int number = rand() % 10 + 1;
Code:int RandomInt(int Min, int Max) { if(Max <= Min) return Min; return Min + rand() % (Max - Min + 1); } int number = RandomInt(1, 10);
Last edited by Magos; 03-11-2005 at 07:47 PM.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Thanks, that seems to have been what I needed. However, now I seem to be having compiler problems. I have done everything as I should have, but my Dev C++ Compiler is unable to compile my program.
Here is the code:
Is the stuff after int main, or the if statement? Or could it be my compiler (Dev C++)????Code:#include <stdio.h> #include <iostream.h> int main(int nNumberofArgs, char* pszArgs[]) { int guess; cout << "I'm thinking of a number between 1 and 10." cout << "\nGuess what it is." cin >> guess; int number = rand() % 10 + 1; if (guess==number) { cout << "Correct!" } else { cout << "Wrong." } return 0; }
Last edited by DZeek; 03-11-2005 at 10:56 PM. Reason: Adding word
You are missing quite a few semicolons in your code. Plus you are using the old style headers.
Code:#include <cstdlib> #include <iostream> using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { int guess; cout << "I'm thinking of a number between 1 and 10."; cout << "\nGuess what it is."; cin >> guess; int number = rand() % 10 + 1; if (guess==number) { cout << "Correct!"; } else { cout << "Wrong."; } return 0; }
Ok that is fixed, but I still have a problem. Whenever I try to compile this program, my Dev C++ compiler says "unable to run program file"
I think I did everything correctly....whats going on?
I've never had that error before, but I googled your error and came up with this FAQ entry:
http://aditsu.freeunixhost.com/dev-cpp-faq.html#runfile
I tried all of that and it still doesn't work... This is starting to get annoying...
EDIT: Now it compiles the program but doesn't create an exe. This is getting frustrating.
Last edited by DZeek; 03-12-2005 at 03:03 AM.
Post your compile log, which can be accessed via the tab of the same name towards the bottom.
You should be using Dev-C++ 4.9.9.x, the most recent version is 4.9.9.2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I believe that means your program is already running dear. You need to close out of that instance of it to be able to run it again. Of course, you don't have a line to stop the program from exiting so the program shouldn't still be open.Originally Posted by DZeek
So what should I change? What is the EXACT code I should use?
alright I written one wich works PERFECTLY in my dev-C++
it it doesn;t work you should re-install your dev-C++ compiler.
Code:#include <iostream> using namespace std; // using standard libary // int main ( int argc , char** argv ) { srand(time(0)); int random_number = rand() % 10 ;// not larger than 10 // cout << random_number << endl; //out puts it // cin.ignore(); // waits for an input and ignores it // }
PLay MystWind beta , within two years
Has C++ changed a lot recently? Because I don't understand most of that....Originally Posted by MystWind
I tried that and compiled it and it works. I add an if statement and a few "cin"s and "cout"s ( ) to get this:
I want to make it so that the person just has to hit enter to exit after it shows "Correct" or "Wrong." cin.ignore(); doesn't seem to do that... any ideas?Code:#include <iostream> using namespace std; int main ( int argc , char** argv ) { srand(time(0)); int random_number = rand() % 10 ; cout << "Guess a number between 1 and 10\n"; int guess; cin >> guess; if (guess==random_number) { cout << "Correct!"; } else if (guess!=random_number) { cout << "Wrong!"; } cin.ignore(); }
cin.get() works.
Or if you want to use the evil system("pause"), go right ahead. system() is defined in stdlib.h or cstdlib.
And no....C++ hasn't changed a lot recently. If you don't understand his example code, you really need to go back to some beginner tutorials dear. If you're confused about the parameters of main(), don't worry...they're not even used in the example.
@Myst Wind: it should be:Code:int random_number = rand() % 11;
Last edited by Krak; 03-12-2005 at 07:48 PM.
I'm confused about the code because the book I was using to learn C++ seems WAY different (and I'm a newbie ).
I tried cin.get(); but it still closes after you guess a number (cin >> guess; ).
Before the window closes, I can see it show "Correct" or "Wrong", but it doesn't ask for input again. It just closes.
That would give a number 0 -> 10, he wanted 1 -> 10.Originally Posted by Krak
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.