# Thread: Random Number Generator?

1. ## 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?

2. 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);```

3. 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++)????

4. 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;
}```

5. 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?

6. I've never had that error before, but I googled your error and came up with this FAQ entry:

7. 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.

8. 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

9. 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.

10. So what should I change? What is the EXACT code I should use?

11. ## 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 //
}```

12. 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 ( ) 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?

13. 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;`

14. 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.

15. 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.

Popular pages Recent additions