couple questions for very small random number program

This is a discussion on couple questions for very small random number program within the C++ Programming forums, part of the General Programming Boards category; Hey I am trying to make a smal random number game just for practice and have 2 questions. First I'm ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    couple questions for very small random number program

    Hey I am trying to make a smal random number game just for practice and have 2 questions. First I'm wondering if im generating the random number (between 1 and 100) the correct way. Second I am wondering how to loop this program (preferably with a for loop) so it will ask more than 1 time and then produce the answer, if the answer isnt correct then I obviously want it to ask then answer again. I am not sure you can use a for with the if-else? Anyways any help will be appreciated, thanks.

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int main ()
    {
       int guess;
       double random;
       int i;
    
    
    
    
    
    
       cout << " I'm guessing of a number between 1 and 100 can you guess it?\n";
       cin >> guess;
    
    
       srand(time(NULL));
       random = rand() % (100 + 1);
    
    
    
    
    
       if (random > guess)
        cout << "Guess was too big\n";
       else if (random < guess)
         cout << "Guess was too small\n";
       else if (random == guess)
         cout << "You got it!\n";
         
         
         
         system ("pause");
         
         return 0;
         
         }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Remove the brackets around 100 + 1. The +1 is supposed to occur after the %100.
    Also, while what you have is fine for one call to rand(), make sure that when you generate more calls to rand that you don't make more calls to srand. Call srand only once in your entire program, preferably near the top of main.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks. was just missing some brackets..but one more thing I can't figure out is why no matter what my program always prints "Yout number is too big". Did I write my else-if wrong or something? Thanks

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;

    int main ()
    {
    int guess;
    double random;
    int i;
    srand(time(NULL));




    for (i = 1; i <=2; i++)
    {
    cout << " I'm guessing of a number between 1 and 100 can you guess it?\n";
    cin >> guess;




    random = rand() %100 + 1;





    if (random > guess)
    cout << "Guess was too big\n";
    else if (random < guess)
    cout << "Guess was too small\n";
    else if (random == guess)
    cout << "You got it!\n";
    }



    system ("pause");

    return 0;

    }
    Last edited by nick753; 01-15-2010 at 01:25 PM. Reason: oops

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    Code:
       
       if (random > guess)
        cout << "Guess was too big\n";
       else if (random < guess)
         cout << "Guess was too small\n";
       else if (random == guess)
         cout << "You got it!\n";
    You need to put 'guess' first.
    As in:
    Code:
       if (guess > random)
        cout << "Guess was too big\n";
       else if (guess < random)
         cout << "Guess was too small\n";
       else if (guess == random)
         cout << "You got it!\n";
    And your random number is going to be between 1 and 101, not 1 and 100.
    A formula for random numbers is: rand() % (max - min) + min
    So that would be: rand() % (100 - 1) + 1
    which could be simplified to rand() % 99 + 1

    You could use a while loop to make the program continue to ask for your guess.

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int main ()
    {
    int guess;
    double random;
    int i;
    srand(time(0)); //Don't use NULL?
    
    cout << " I'm guessing of a number between 1 and 100 can you guess it?\n";
    random = rand() % 99 + 1;
    
    while (true) //Loop infinitely (or until a 'break' command is called)
    {
    cin >> guess; //Ask for input
    
    
    if (guess > random)
       cout << "Guess was too big\n";
    else if (guess < random)
       cout << "Guess was too small\n";
    else
    {
       cout << "You got it!\n";
       break; //Break out of the while loop!
    }
    } //End bracket for while loop
    
    
    system("PAUSE");
    
    return 0;
    
    }

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    but one more thing I can't figure out is why no matter what my program always prints "Yout number is too big". Did I write my else-if wrong or something? Thanks
    This is because you keep resetting the value of the 'random' variable with a random number after the user guesses.
    I fixed this in the code I gave you. But if you want to still use your code, then put the
    random = rand() %100 + 1;
    before the user guesses AND outside of the for loop (so that it is initialized with a random value only once).
    Last edited by Flaug; 01-15-2010 at 02:27 PM.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks guys got it figured out.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by Flaug View Post
    You And your random number is going to be between 1 and 101, not 1 and 100.
    A formula for random numbers is: rand() % (max - min) + min
    So that would be: rand() % (100 - 1) + 1
    which could be simplified to rand() % 99 + 1
    Incorrect!
    The formula is rand() % (max - min + 1) + min

    rand() % 100 gives you a number from 0 to 99, and adding one gives you 1 to 100.

    Also, when you have to use comments like
    Code:
    //End bracket for while loop
    You've either failed at doing correct indentation, or at keeping your function to an appropriate length, or both.
    Last edited by iMalc; 01-15-2010 at 06:39 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    You've either failed at doing correct indentation, or at keeping your function to an appropriate length, or both.
    Or I'm just too lazy to press the spacebar twenty times to indent the code while in the reply text box.

    Incorrect!
    The formula is rand() % (max - min + 1) + min
    Whoops! Guess I screwed that one up.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. A couple more small questions...
    By Ash1981 in forum C Programming
    Replies: 7
    Last Post: 12-31-2005, 09:36 AM
  3. Program Random Questions
    By mikeprogram in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2005, 10:45 PM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  5. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 09:52 AM

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