Random numbers with modern c++ conventions

Hey Everyone,

I was converting some code to practice a random number generator method I was reading about online. Strange thing is, It doesn't seem to work with numbers over 99. Even if I change the initial high number to 200 or above, it still hits a limit at 99 if the input number is 100+. I know I 'must be missing something rather obvious. Can anyone help me by pointing it out?

Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;

int main()
{
    cout << "Welcome to the Computer Number Guessing Game" << endl << endl;
    int myNumber = 0;
    cout << "Please enter a number (MAX 100): ";
    cin >> myNumber;
    cout << "\nWould you like the computer to AutoFind your number? (y,n): ";
    char autoFind = 'y';
    cin >> autoFind;
    int high = 100, low = 0;
    if(autoFind == 'y'){
        //srand(static_cast<unsigned int>(time(0))); // seeds rndm # Gen
        //int guess = rand() % high + low; // Remainder of rand # / 100 + 1
        random_device rd;
        mt19937 eng(rd());
        uniform_int_distribution<> distr(low, high);
        cout << "\nGuessing... " << guess << endl;
        do {
            while(guess > myNumber){
                high = guess;
                cout << "Too High, reguessing lower...";
                guess = distr(eng);
                //guess = rand() % (high - low) + (low + 1);
                if(guess == high)
                    guess -= 1;
                if(guess == low)
                    guess += 1;f
                cout << guess;
                cout << endl;
            }
            while(guess < myNumber){
                low = guess;
                cout << "Too Low, reguessing higher...";
                guess = distr(eng);
               //guess = rand() % (high - low) + (low + 1);
                if(guess == low)
                    guess += 1;
                if(guess == high)
                    guess -= 1
                cout << guess;
                cout << endl;
            }
        } while (guess != myNumber);
        cout << "\nI got it! Your number is: " << guess << "!" << endl;
    }
    return 0;
}
Rasta,
please clean up your code and make it as short as possible to still contain the issue.
In its current state the code will not compile. There is
- an erroneous f in the 1st while loop,
- a missing ; in the second while loop,
- the initialization of 'guess' is commented out before it is used.

Once this is done, I can look at it again. But there is also a chance it will help you sort out what is wrong as well.
Vitaliy
vitalishe wrote:
the initialization of 'guess' is commented out before it is used.
No, it's not. It is assigned a value on the previous line.
Last edited on
LOL! Yes, it is!

You first use 'guess' on line #23 and the place where it was "defined" as int is on line #19, which is commented out :-)

If you can make the code compile most of the questions will be sorted out.
Thanks for the replies.

Sorry for the unclean code, to be honest it was late, I was tired, and it was compiling for me (Well sort of) so I assumed it was fine.

Now that I look at it with fresh eyes, there is no reason it should have compiled. Yesterday, I had just installed the latest version of CodeBlocks and was trying to use the new c++ 11 capabilities.

I now realize there is something wrong with my compiler or the way I have it set up.

error: unrecognized command line option "-std=c++11"

I will be looking into this...

So I figured out that I was having compiler issues, I re-installed and now everything is working great - rather it didn't compile as it was posted, just as it shouldn't have.

Cleaned it up, now its compiling, but I am getting a runtime error now.

"Terminate called after throwing an instance of 'std::runtime_error'
what(): random_device::random_device(const std::string&)

I think my problem is that I don't understand how this method generates random numbers. I read that it - Obtains a random number from Hardware, then you seed the generator, define a range, and than generate the numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <random>
using namespace std;

int main()
{
    cout << "Welcome to the Computer Number Guessing Game" << endl << endl;
    int myNumber = 0;
    cout << "Please enter a number (MAX 100): ";
    cin >> myNumber;
    cout << "\nWould you like the computer to AutoFind your number? (y,n): ";
    char autoFind = 'y';
    cin >> autoFind;
    int high = 150, low = 1;
    if(autoFind == 'y'){
        random_device rd;
        mt19937 eng(rd());
        uniform_int_distribution<> distr(low, high);
        int guess = distr(eng);
        cout << "\nGuessing... " << guess << endl;
        do {
            while(guess > myNumber){
                high = guess;
                cout << "Too High, reguessing lower...";
                guess = distr(eng);
                cout << guess;
                cout << endl;
            }
             while(guess < myNumber){
                low = guess;
                cout << "Too Low, reguessing higher...";
                guess = distr(eng);
                cout << guess;
                cout << endl;
            }
        } while (guess != myNumber);
        cout << "\nI got it! Your number is: " << guess << "!" << endl;
    }
    return 0;
}

Works as posted.
I've seen that exception thrown when attempting to use std::random_device on ideone.com, where hardware access is blocked for security reasons.

If you have the same problem, you may have to fall back to the age-old time(NULL) for the seed.
Thanks for your post. I can stop troubleshooting now!

Topic archived. No new replies allowed.