# Random Number Generator?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-11-2005
DZeek
Random Number Generator?
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?
• 03-11-2005
Magos
rand() gives a random integer between 0 and RAND_MAX. Use modulus (a % b) on the result to cut it into your desired interval.
Code:

`int number = rand() % 10 + 1;`
Or as a more general function:
Code:

```int RandomInt(int Min, int Max) {         if(Max <= Min) return Min;         return Min + rand() % (Max - Min + 1); } int number = RandomInt(1, 10);```
• 03-11-2005
DZeek
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:

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; }```
Is the stuff after int main, or the if statement? Or could it be my compiler (Dev C++)???? :confused:
• 03-11-2005
swoopy
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; }```
• 03-11-2005
DZeek
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?
• 03-12-2005
swoopy
I've never had that error before, but I googled your error and came up with this FAQ entry:
• 03-12-2005
DZeek
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.
• 03-12-2005
laserlight
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
• 03-12-2005
Krak
Quote:

Originally Posted by DZeek
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 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.
• 03-12-2005
DZeek
So what should I change? What is the EXACT code I should use?
• 03-12-2005
MystWind
mine
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 // }```
• 03-12-2005
DZeek
Quote:

Originally Posted by MystWind
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 // }```

Has C++ changed a lot recently? Because I don't understand most of that....

I tried that and compiled it and it works. I add an if statement and a few "cin"s and "cout"s ( :p ) to get this:

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(); }```
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?
• 03-12-2005
Krak
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;`
• 03-12-2005
DZeek
I'm confused about the code because the book I was using to learn C++ seems WAY different (and I'm a newbie :D).

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.
• 03-12-2005
Magos
Quote:

Originally Posted by Krak
@Myst Wind: it should be:
Code:

` int random_number = rand() % 11;`

That would give a number 0 -> 10, he wanted 1 -> 10.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last