[SOLVED] a little randomize problem... Chili help?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

[SOLVED] a little randomize problem... Chili help?

Post by Natox » January 21st, 2012, 1:05 pm

Hey chili, I know this is not part of your tutorials so don't feel obligated to help me.
But I have the following problem... I'm trying to make a small program in which I type in a random number anywhere between 1 and 10 and the computer has to guess it by throwing out a randomly generated number with time seed random % 10 + 1.
Now, as I run the program, it does loop the random guess and all of it's if conditions, but it seems to produce the same random number over-and-over-and-over.... despite the fact that I specifically told the program to take a truely random number.... Could you have a look at it?

There is no compilation errors, what so ever.

Code: Select all

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

using namespace std;

int main()
{
    srand(time(NULL));
    int randomGuess = rand() % 10 + 1;
    int tries = 0, myInput;

    cout << "\n\tWelcome to GUESS MY NUMBER!\n";
    cout << "\tPick a number in between 1 and 10\n";
    cout << "\tand I will try to guess it!\n\n";

    cout << "Please enter a number between 1 and 10: ";
    cin >> myInput;


    do{

        cout << "My guess is " << randomGuess << ".\n";
        ++tries;

        if(randomGuess < myInput)
        {
            cout << "I guessed too low!\n\n";
        }
        if(randomGuess > myInput)
        {
            cout << "I guessed too high!\n\n";
        }
    }while(randomGuess != myInput);

    cout << "I think I guessed it right in " << tries << " guesses!\n";

    return 0;
}

It does work when I define 'randomGuess' again inside the do loop... but is this really needed?

Code: Select all

cout << "My guess is " << (randomGuess = rand() % 10 + 1)<< ".\n";
Last edited by Natox on January 22nd, 2012, 4:31 pm, edited 1 time in total.
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

User avatar
chili
Site Admin
Posts: 3939
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: a little randomize problem... Chili help?

Post by chili » January 21st, 2012, 4:07 pm

No problem Natox. I see what you're doing wrong and could just tell you, but what I recommend is that you run the debugger on your code, single step through it, and see for yourself why it does not work.

If you still really want me to give you the answer, I will, but I think this would be a perfect opportunity for you to work with the debugger.

Good luck! :)
Chili

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

Re: a little randomize problem... Chili help?

Post by Natox » January 21st, 2012, 11:18 pm

The debugger shows that the value of the randomGuess integer does not change when the loop is re-loaded but the tries integer does increase. I think it has something to do with the fact that it generates a random number while initialized at the top of the code.... and thus the loop never reaches the random int initializer, so it can never re-load another random value into that integer... am I right about that? Is that how it works?

In that case it would be more clever to call that rand() function inside the loop itself right?
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

User avatar
chili
Site Admin
Posts: 3939
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: a little randomize problem... Chili help?

Post by chili » January 22nd, 2012, 2:46 am

Looks like you've pretty much figured it all out on your own Natox, as I knew you would. I think you may have been confusing the initialization of the pseudo-random number generating module with the generation of random numbers.

The function srand() seeds the random number generator, determining which sequence of random numbers will be generated. If I'm not mistaken, when you don't call srand() with some arbitrary seed number, you will always get the same default sequence on every run of the program. Not very random at all.

You need to call rand() to generate the random number. Since you were only calling it once in the initialization section of your code and then assigning the value returned by rand() to randomGuess, the value of randomGuess remains the same for every run of the loop. Generally, if you want the contents of some variable to vary inside a loop you must change those contents within said loop.
Natox wrote:In that case it would be more clever to call that rand() function inside the loop itself right?
I would say rather that it is essential that you do so. ;)

My recommendation:

Code: Select all

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

using namespace std;

int main()
{
    srand(time(NULL));
    int randomGuess = 0,
        tries = 0,
        myInput = 0;

    cout << "\n\tWelcome to GUESS MY NUMBER!\n";
    cout << "\tPick a number in between 1 and 10\n";
    cout << "\tand I will try to guess it!\n\n";

    cout << "Please enter a number between 1 and 10: ";
    cin >> myInput;


    do{
        randomGuess = rand() % 10 + 1;
        cout << "My guess is " << randomGuess << ".\n";
        ++tries;

        if(randomGuess < myInput)
        {
            cout << "I guessed too low!\n\n";
        }
        if(randomGuess > myInput)
        {
            cout << "I guessed too high!\n\n";
        }
    }while(randomGuess != myInput);

    cout << "I think I guessed it right in " << tries << " guesses!\n";

    return 0;
}
Chili

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

Re: a little randomize problem... Chili help?

Post by Natox » January 22nd, 2012, 10:22 am

Looks like you've pretty much figured it all out on your own Natox, as I knew you would.

Thanks for your trust in me mate!
You know the thing is, since my knowledge of C++ is lesser than basic at this time, it was quite hard to figure out what could have been wrong with this. Since this was one of those errors that wasn't wrong according to the compiler, right? And I do not yet know all the things you can/should do and can't/shouldn't do in the language of C++. So it all comes down to logic sense... that's the way I figured out what could go wrong.... but I still remained pretty uncertain until I had answers from an expert, because of the lack of knowledge.....You do know what I mean right?

If I'm not mistaken, when you don't call srand() with some arbitrary seed number, you will always get the same default sequence on every run of the program. Not very random at all.

You are absolutely right on this one... I have been told that the computer can only follow a certain algorithm to generate random numbers on it's own. And because it follows the same approach (algorithm) each time it generates random numbers on it's own, the outcome is the same over and over again..

My recommendation:

Thank you sir, this is exactly how I solved it aswell... and it does work perfectly.
And now I do understand that once you seeded a true random generator in the top of the 'main' body it can be called anywhere inside the 'main' body code. So there is no need to initialize a 'variable INCLUDING the random generator assignment' in the top of the main.
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

Post Reply