Thread: Random number generation

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Question Random number generation

    I am trying to write a piece of code using a random number generation result. I have not been able to successfully debug (see below). Could you assist? I am very new to this programming language.

    // Guess - Number Game
    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;

    #include <cstdlib>

    int main()
    {

    int guessCounter = 1,
    x;


    for (int guess = 1; guess <=20; guess++) {
    x = 1 + rand() % 1;

    cout <<"I've got the number. Please guess it:";
    cin >> guess;


    if (guess < x)
    cout << "It's less than the number. Please guess again:";

    if (guess > x)
    cout << "It's greater than the number. Please guess again:";
    else
    break;
    guessCounter = guessCounter+ 1;
    cout<<"Input next guess.";

    if (guess == x)
    cout << "You guessed it!"<<endl;

    }
    return 0;

    }

  2. #2
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65

    WHOA!

    You made this way too complex. First off, it doesnt do it quite corectly. I runs it 20 times regardless if the guess it right or not. Here, I wil hep you by giving you some usefull code. It may not all be correct in the random part, because i am not changing that. What compiler are you using?

    Code:
    #include <iostream> 
    #include <cstdlib>
    
    using std::cout; 
    using std::cin; 
    using std::endl; 
    
    int main() 
    { 
    
    int guessCounter = 0;
    int x = 1 + rand() % 1; 
    
    cout <<"I've got the number. Please guess it:";
    
    while (guess != x)
    {
    cin >> guess; 
    
    if (guess < x) 
    cout << "It's less than the number. Please guess again:"; 
    
    if (guess > x) 
    cout << "It's greater than the number. Please guess again:"; 
    
    if (guess == x) 
    cout << "You guessed it!"<<endl;
    
    guessCounter = guessCounter+ 1; 
    
    cout<<"Input next guess.";
    }
    return 0; 
    }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    // Guess - Number Game 
    #include <iostream> 
    #include <cstdlib>
    #include <ctime>
    
    using std::cout; 
    using std::cin; 
    using std::endl; 
    
    int main() 
    { 
        int guessCounter, x, guess;
    
        // Only get a single random number, otherwise you are getting
        // a new one each time you go through the loop.  Also, you need
        // to seed the random number generator otherwise you get the
        // same "random" number each time you run the program.  Last,
        // what you had will only give 1 as the number, try picking something
        // larger to do the "%" with, for example, this will give a number
        // from 1 to 100.
    
        srand( time(NULL) );
        x = 1 + rand() % 100;
        cout <<"I've got the number. Please guess it:"; 
    
        for( guessCounter = 0; guessCounter < 20; guessCounter++ )
        { 
            cin >> guess; 
    
            if (guess < x) 
                cout << "Your guess is less than the number. Please guess again:"; 
    
            else if (guess > x) 
                cout << "Your guess is greater than the number. Please guess again:";
            else if (guess == x) 
            {
                cout << "You guessed it!"<<endl; 
                break;
            }
        } 
    
        return 0; 
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Try this:

    PHP Code:
    #include <iostream> 
    #include <cstdlib> 
    #include <ctime>

    using namespace std;

    int main() 

        
        
    int guessCounter 1
        
    int x 0
        
    int guess 0;

        
    srandtime(NULL) );


        
    rand() % 1// will give you a random number from 1 to 2 (?)
        
        
    cout <<"I've got the number. Please guess it:"
        

        for( 
    guessCounter 0guessCounter 20guessCounter++ )
        { 
            
    cin >> guess


            if (
    guess x
            {
                
    cout << "It's less than the number. Please guess again:"
            }
            else if (
    guess x
            {
                
    cout << "It's greater than the number. Please guess again:"
            }
            else
            {
                
    // if it's neither < nor >, it should be ==
                
    break;
            }            

            
    cout<<"Input next guess."
        }

        if (
    guess == x
        {
            
    cout << "You guessed it!"<<endl
        }
        else
        {
            
    cout << "You lost. It was " << << endl;

        } 
        
        return 
    0

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    yeah, what nvoigt put would work, but you'll only get 19 guesses with that, lol. he meant '<=20' i think. or you can do this:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
        int guesses=1;
        int guess=0;
        int x=0;
    
        srand(TIME(NULL));
    
        x=1 + rand() % (5-1+1); //in between 1 and 5
    
        do
        {
            cout<<"Enter your guess: ";
            cin>>guess;
    
            if(guess>x)
                cout<<"Too high. Guess again."<<endl;
            if(guess<x)
                cout<<"Too low. Guess again."<<endl;
            if(guess==x)
                break;
    
            guesses++
        }while(guesses<=20);
    
        if(guesses==21)
            cout<<"You lost"<<endl;
        if(guesses<21)
            cout<<"You won"<<endl;
    
        return 0;
    }

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. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  3. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generation
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM