Thread: I need help with a random number generator program.

  1. #1
    Registered User
    Join Date
    Aug 2013

    I need help with a random number generator program.

    I am trying to write a simple program that picks a number between 1 and 100, and then lets the user guess what the number is. The program should tell the user if their guess is to high, to low, or correct.

    The problem I am having a problem with the program I wrote. Every time I input a number it tells me I am correct even if that number is larger than 100.

    I don't what the problem is. Could someone please help.

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int rand_range(int low, int high)
        return rand() % (high - low + 1 )+low;
    int main()
    {   int input;
        cout << "pick a number between 1 and 100 \n";
        cin >> input;
            cout << "correct!\n";
        else if( input != rand_range(1,100))
            cout<< "wrong!\n";

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The difference between "=" and "==" rears its ugly head yet again.

  3. #3
    Registered User
    Join Date
    Apr 2006
    You're also calling rand_range too many times. You should only call it once.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jul 2013
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,6);
    int dice_roll = distribution(generator);  // generates number in the range 1..6
    If you would like to know others ways to produce random numbers take a look at: <random> - C++ Reference

    There is a whole library on random number generation, check it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator Stuck on 1 Number?
    By Dollydaydream in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2013, 08:43 AM
  2. Replies: 6
    Last Post: 02-02-2012, 06:14 AM
  3. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  4. Random Number Generator Program...PLEASE HELP!!
    By girly_engineer in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2010, 11:23 PM
  5. C++ program random number generator need help
    By catdieselpow in forum C++ Programming
    Replies: 15
    Last Post: 10-07-2007, 04:53 PM