Random Number Generator?

This is a discussion on Random Number Generator? within the C++ Programming forums, part of the General Programming Boards category; I am creating a simple program which is in need of a simple random number generator. How would I make ...

  1. #1
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23

    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. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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);
    Last edited by Magos; 03-11-2005 at 06: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.

  3. #3
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    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++)????
    Last edited by DZeek; 03-11-2005 at 09:56 PM. Reason: Adding word

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #5
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    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. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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

  7. #7
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    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 02:03 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    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.

  10. #10
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    So what should I change? What is the EXACT code I should use?

  11. #11
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Lightbulb 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 //
    }
    PLay MystWind beta , within two years

  12. #12
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    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 ( ) 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. #13
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    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 06:48 PM.

  14. #14
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    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. #15
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 05:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21