Troubles with getting random number inside "while" loop using structures.

Hello everybody. I'm writing a program (actually a game) about fighting with opponents on the arena. The problem is that I get always the same random numbers on each "while" loop. I created a structure in the following header, which contains information about fighters:

1
2
3
4
5
6
7
8
9
10
11
#ifndef COMPETITOR_H_INCLUDED
#define COMPETITOR_H_INCLUDED

struct competitor
{
    std::string name;
    int health;
    int attack;
};

#endif // COMPETITOR_H_INCLUDED 


And here is rest of my code:

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <competitor.h>
#include <conio2.h>

using namespace std;

int new_game();
int arena();
int fight();

int ID = 0;

competitor player;

int main()
{
    srand(time(NULL));

    cout << endl << "ARENA" << endl;
    cout << endl << "[1] New Game";
    cout << endl << "[2] Exit" << endl << endl;
    cout << "Option: ";

    int option;

    cin >> option;

    if (option == 1) new_game();

    else if (option == 2) break;

    else
    {
       cout << "Option: ";
       cin >> option;
    }

    return 0;
}

int new_game()
{
    clrscr();

    cout << "Enter your character's name: ";
    cin >> player.name;

    player.health = 25;
    player.attack = (rand()%6)+1;

    arena();
}

int arena()
{
    clrscr();

    cout << "[1] Next fight" << endl;
    cout << "[2] Return to menu" << endl;
    cout << endl << "Option: ";

    int option;

    cin >> option;

    if (option == 1) fight();
    else if (option == 2) main();
    else
    {
       cout << "Option: ";
       cin >> option;
    }
}

int fight()
{
    clrscr();

    int number = 1;

    competitor opponent[2];

    opponent[0] = {"Rat", 6, (rand()%4)+1};
    opponent[1] = {"Drunkard", 10, (rand()%6)+1};


    cout << "Your opponent is " << opponent[ID].name;
    cout << endl << "Your opponent's health: " << opponent[ID].health;
    cout << endl << "Your health: " << player.health << endl;

    while (opponent[ID].health > 0 && player.health > 0)
        {
            cout << endl << endl << "Round " << number << endl;

            cout << opponent[ID].name << " inflicts" << opponent[ID].attack << " damage, ";
            cout << "you have " << (player.health = player.health - opponent[ID].attack) << " health points" << endl;

            if (player.health <= 0) break;

            cout << player.name << " inflicts " << player.attack << " damage, ";
            cout << "your opponent has " << (opponent[ID].health = opponent[ID].health - player.attack) << " health points" << endl;

            number++;
        }

    if (player.health > opponent[ID].health)
        {
            cout << endl << "Congratulations! You managed to defeat your opponent. Prepare for the next fight.";
            ID++;
            getch();
            arena();
        }

     else
        {
            cout << endl << "Unfortunately, you have been defeated. Start again.";
            ID = 0;
            getch();
            main();
        }

    }



Basically it works, on each program run I get different attack values, but in every round (on every "while" loop) they are always the same. I have no idea, how to solve this problem. All help is greatly appreciated.

Regards
Hey there,

If you randomize this number once at the start of each game then it remains constant and does not change so it is no surprise that you are getting the same attack damage for each round. The best thing to do would be to create a OnNewRound() function and randomize the attack values inside there so it can be called before each attack (or round) thus making the attack values different.

To sum up: you need to randomize the attack values once per round/attack and not one per game

I hope I have understood the question correctly, if not then please feel free to reply and I will do my best to get back to you and leave as much help as possible.

Thanks,
Garry++
Thank you for help, your reply gave me a point. But I managed to do it in another way though. I've modified the "while" loop and it looks like this now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (opponent[ID].health > 0 && player.health > 0)
        {
            cout << endl << endl << "Round " << number << endl;

            int AttackCPU = (rand()%opponent[ID].attack)+1);
            cout << opponent[ID].name << " inflicts" << AttackCPU << " damage, ";
            cout << "you have " << (player.health = player.health - AttackCPU) << " health points" << endl;

            if (player.health <= 0) break;

            int AttackHuman = (rand()%player.attack)+1;
            cout << player.name << " inflicts " << AttackHuman << " damage, ";
            cout << "your opponent has " << (opponent[ID].health = opponent[ID].health - AttackHuman) << " health points" << endl;

            number++;
        }


And I declared opponents as:

opponent[0] = {"Rat", 6, 4};

instead of

opponent[0] = {"Rat", 6, (rand()%4)+1};.

So it works like I wanted, but randomness of numbers could be better. I must find better random number generator.

Regards
Topic archived. No new replies allowed.