Random array picker not working the way I wanted, send help

I made a "Guess the secret word game" in a do-while loop but every time you try again the secret word changes. It worked but, My problem is that every time I stop the program and run it again the secret word is still the same it only changes when I try it again.

When I run the program the secret word is "Micheal De Santa" when I stop the program and run it again it is still the same, It only changes when I lose or win and I try it again on the do-while...

please help me.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
#include <cctype> // ::to_lower
#include <limits> // numeric_limits
#include <algorithm>
#include <random>
#include <cstdlib>
#include <ctime>

int main()
{

    char rerun_option{ 'n' };

    std::cout << "GUESSING GAME" << std::endl;


    do
    {

        static const std::string secword[6] = { "Arthur Morgan", "Micheal De Santa", "Ezio Auditore", "Dutch Van der Linde", "Aiden Pierce", "Marcus Halloway" };

        // Seed the random generator
        srand(time(0));
        
        // Pick element in array(secword) pseudo - randomly
        std::string secretword = secword[rand() % 6];

        std::string guess{ };
        int guessCount{ };
        int guessLimit{ 3 };
        bool outofGuesses{ false };

        // Input if the guess is right & checks guess limit
        while (secretword != guess && !outofGuesses)
        {
            if (guessCount < guessLimit)
            {
                std::cout << "Enter video game character name guess: ";
                std::getline(std::cin, guess);
                
                // hint command
                if (guess == "/hint") {
                    if (secretword == "Arthur Morgan" || secretword == "Dutch Van der Linde") {
                        std::cout << "\nHint: Red Dead Redemption 2\n\n";
                    }
                    else if (secretword == "Micheal De Santa") {
                        std::cout << "\nHint: Grand Theft Auto V\n\n";
                    }
                    else if (secretword == "Ezio Auditore") {
                        std::cout << "\nHint: Assassin's Creed 2\n\n";
                    }
                    else {
                        std::cout << "\nHint: Watch_dogs\n\n";
                    }
                }
                else {
                    guessCount++;
                }

            }
            else
            {
                outofGuesses = true;
            }
        }


        if (outofGuesses) // You win or You Lose
        {
            std::cout << "\nYou Lose!\n\n";
            outofGuesses = false;
            std::cout << "Correct Answer: " << secretword << "\n\n";
        }
        else
        {
            std::cout << "\n*** You Win! ***\n\n";
        }

        std::cout << "Try Again?(Y/N) ";
        std::cin >> rerun_option;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // clear input
        std::cout << '\n';
    } while (::tolower(rerun_option) == 'y');


    return 0;
}
Last edited on
Hello yvez,

For your header file yo do not need "<stdlib.h" and "<time.h>" because you are using the C++ versions which are better.

On line 23 you are using "rand()" to choose, but you did not seed the RNG with "srand" before you use "rand". This will always generate the same numbers in the same order.

Add srand(static_cast<unsigned int>(time(nullptr))); before your first call to "rand".

while (::tolower(rerun_option) == 'y');. This may work, but put the "std" at he beginning like you have everywhere else.

You may also find these useful:

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

Andy
If you are going to use the C library rand() function you need to seed the random number generator with srand(). One time before you generate a random number. Preferably near the top of your main function.
https://en.cppreference.com/w/c/numeric/random/srand

Without seeding the PRNG you will get the same sequence.
It is solved now, thanks to Handy Andy now I know I need to seed the rand first. <3
Topic archived. No new replies allowed.