39

Possible Duplicate:
Generating random integer from a range

I am trying to create a program where the computer guesses a number the user has in his/her mind. The only user input required is whether the guess was too high, too low, or correct. I'm having a problem generating a random number between two variables that store the min and max based on previous guesses. Here is my code:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>

    using namespace std;

    int main()
    {
        srand(static_cast <unsigned int> (time(0)));

        int compGuess = rand() % 100 +1; //Generates number between 1 - 100
        int highestNumber = 100;
        int lowestNumber = 1;
        char ready;
        char highLowSuccess;
        bool success;
        int tries = 0;


        cout << "Please pick a number between 1 - 100. I will guess your number. Don't tell me what it is!\n\n";


        do
        {
            cout << "Are you ready? (y/n)\n\n";
            cin >> ready;

            if (ready == 'y')
            {
                do
                {
                    cout << "Is your number " << compGuess << "?\n\n";
                    cout << "High, Low or Success?";
                    ++tries;
                    cin >> highLowSuccess; //User input telling the computer whether its too high, too low, or a success

                    if (highLowSuccess == 'h') //Executes code if number guessed was too high.
                    {

                        highestNumber = compGuess - 1; //Stores variable indicating the highest possible number based on user input
                        compGuess = rand() % highestNumber +1; //Generates a new random number between 1 and the new highest possible number
                        success = false;
                    }

                    else if (highLowSuccess == 'l') //Executes code if number guessed was too low.
                    {
                        lowestNumber = compGuess + 1;//Stores variable indicating the lowest possible number based on user input
                        compGuess = (rand() % highestNumber - lowestNumber + 1) + lowestNumber // <---- Not producing the desired result
                        success = false;
                    }

                    else if (highLowSuccess == 's') //Executes code if the computer's guess was correct.
                    {
                        cout << "I guessed your number! It only took me " << tries << " tries!";
                        success = true;
                    }


                } while (success != true);
            }


            else
            {
             continue;
            }

       } while (ready != 'y');



    return 0;

    }

highestNumber is what the max should be and lowestNumber is what the min should be. I need an equation that lets me generate a random number while taking the highest and lowest possible numbers into account.

Forgive me if the answer is really simple, I'm a noob programmer. xD

Community
  • 1
  • 1
Jammin
  • 403
  • 1
  • 4
  • 4
  • Replace `100` with `highestNumber` and replace `1` with `lowestNumber`? – Blender Sep 30 '12 at 01:38
  • And besides being a dupe, it looks like you were on the right track anyway. You just have an order-of-operations problem on your line marked "not producing the desired result". Parenthesize correctly and you'll be good. There are many example answers here. – Carl Norum Sep 30 '12 at 02:26
  • 2
    @CarlNorum : Sorry for the duplicate. =( – Jammin Sep 30 '12 at 03:28

4 Answers4

76

To generate a random number between min and max, use:

int randNum = rand()%(max-min + 1) + min;

(Includes max and min)

Sidharth Mudgal
  • 4,234
  • 19
  • 25
  • 10
    This answer is probably good enough for most uses, but the OP should be aware that this method has a bit of a bias towards the low end if the range of `rand()` isn't divisible by `max - min + 1`. – Carl Norum Sep 30 '12 at 02:07
  • Could you tell me a little more about it? What does the "range of rand()" mean? – Sidharth Mudgal Sep 30 '12 at 02:10
  • 13
    `rand` returns numbers between `0` and `RAND_MAX`. That's `RAND_MAX + 1` possible input values to your modulo. There are `max - min + 1` possible output values. If the ratio of inputs to outputs isn't an integer, then you're ending up with a small bias towards the lower numbers. Imagine writing out all `RAND_MAX + 1` inputs: `0, 1, 2, ... RAND_MAX`, right? Now write out underneath them the post-modulo ones (for `min = 0` and `max = 5`), say: `0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5`, etc. Now if `RAND_MAX` isn't divisible by `6`, you get a partial cycle `0, 1, 2` ... – Carl Norum Sep 30 '12 at 02:13
  • 7
    ... or something like at the end. That means there are more `0, 1, 2` outputs than `3, 4, 5` outputs - giving your algorithm a bias towards the `0, 1, 2` answers. – Carl Norum Sep 30 '12 at 02:14
  • Ah! I get your point. Thanks. – Sidharth Mudgal Sep 30 '12 at 02:20
  • How to get random floating point number between `min` and `max`? – Beginner Feb 06 '18 at 15:56
  • is there a way to fix the issue @CarlNorum has pointed ? – cerofrais May 12 '23 at 17:50
  • Several other answers here cover that. – Carl Norum May 12 '23 at 18:12
37

Really fast, really easy:

srand(time(NULL)); // Seed the time
int finalNum = rand()%(max-min+1)+min; // Generate the number, assign to variable.

And that is it. However, this is biased towards the lower end, but if you are using C++ TR1/C++11 you can do it using the random header to avoid that bias like so:

#include <random>

std::mt19937 rng(seed);
std::uniform_int_distribution<int> gen(min, max); // uniform, unbiased

int r = gen(rng);

But you can also remove the bias in normal C++ like this:

int rangeRandomAlg2 (int min, int max){
    int n = max - min + 1;
    int remainder = RAND_MAX % n;
    int x;
    do{
        x = rand();
    }while (x >= RAND_MAX - remainder);
    return min + x % n;
}

and that was gotten from this post.

Community
  • 1
  • 1
Rivasa
  • 6,510
  • 3
  • 35
  • 64
28

If you have a C++11 compiler you can prepare yourself for the future by using c++'s pseudo random number faculties:

//make sure to include the random number generators and such
#include <random>
//the random device that will seed the generator
std::random_device seeder;
//then make a mersenne twister engine
std::mt19937 engine(seeder());
//then the easy part... the distribution
std::uniform_int_distribution<int> dist(min, max);
//then just generate the integer like this:
int compGuess = dist(engine);

That might be slightly easier to grasp, being you don't have to do anything involving modulos and crap... although it requires more code, it's always nice to know some new C++ stuff...

Hope this helps - Luke

  • Sadly, I'm not using a C++11 compiler. Sorry, I guess I should have mentioned my compiler in the OP – Jammin Sep 30 '12 at 03:31
8
rand() % ((highestNumber - lowestNumber) + 1) + lowestNumber
Darius Makaitis
  • 880
  • 1
  • 5
  • 5
  • This answer is probably good enough for most uses, but the OP should be aware that this method has a bit of a bias towards the low end if the range of `rand()` isn't divisible by `highestNumber - lowestNumber + 1`. – Carl Norum Sep 30 '12 at 02:08