Pseudo Random Numbers in While Loop

Hey guys, I'm trying to create a simple game that is a duel between a user and dragon. The idea stemmed from the codecademy challenge and I decided to add a hit or miss chance as well as a health factor. When I run the code, it runs (Yay! Woo!) unfortunately, the program returns the same damage values for the same iteration of loops. If you run the code, on the first cycle, it always misses the attack. On the second it is always misses and you take damage, on the third, the user deals 39 damage, etc. It is ALWAYS the same and I'm not sure why. Can someone give me a hint or tell me why it's always looping the same per program execution?


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
  #include <iostream>
#include <string>

using namespace std;
 
int main() {
	bool slayer = true;
	string userChoice;
	int userHealth = 100;
	int dragonHealth = 500;
	int userDamageThisRound; //= rand() % 80 + 1;
	int totalDamage = 0;
	int userHitOrMiss; //= rand() % 50 + 1;
	int dragonHitOrMiss; //= rand() % 100 + 1;
	int dragonDamageThisRound;

	while(slayer) {
		cout << "Do you want to attack the dragon or evade it?" << endl;
		getline (cin, userChoice);
		if (userChoice == "attack") {
            userHitOrMiss = rand() % 50 + 1;
			if (userHitOrMiss <= 30) {
				cout << "You hit the dragon!" << endl;
				userDamageThisRound = rand() % 80 + 1;
				dragonHealth = dragonHealth - userDamageThisRound;
				cout << "You dealt " << userDamageThisRound << " to the dragon!" << endl;
				if (dragonHealth <= 0) {
					cout << "Congrats! You slew the Dragon!" << endl;
					slayer = false;
				} else {
					cout << "The Dragon has " << dragonHealth << " health remaining" << endl;
					continue;
				}
			} else if (userHitOrMiss > 30) {
				cout << "Oops! You missed!" << endl;
				userDamageThisRound = 0;
				}
			dragonHitOrMiss = rand() % 100 + 1;
			if (dragonHitOrMiss <= 20) {
				cout << "The Dragon hit you!" << endl;
				dragonDamageThisRound = rand() % 50 + 1;
				userHealth = userHealth - dragonDamageThisRound;
				cout << "the Dragon dealt " << dragonDamageThisRound << " damage to you!" << endl;
    				if (userHealth <= 0) {
                        cout << "You were killed by the Dragon!" << endl;
                        slayer = false;
                    } else {
                        cout << "You have " << userHealth << " remaining!" << endl;
                        continue;
                    }
			} else {
				cout << "The Dragon's attack missed!" << endl;
				}
		} else if (userChoice == "evade") {
			userDamageThisRound = 0;
			dragonDamageThisRound = 0;
			cout << "You avoided the dragon!" << endl;
			cout << "You did " << userDamageThisRound << " damage!" << endl;
			cout << "The Dragon did " << dragonDamageThisRound << " damage!" << endl;
			cout << "Your Health: " << userHealth << endl;
			cout << "Dragon Health: " << dragonHealth << endl;
		} else {
			cout << "That is not a valid choice, please choose attack or evade."<< endl;
			continue;
            }
		} system("pause");
        return 0;
}
Haven't run your program, but it sounds like this is the problem:
http://www.cplusplus.com/reference/cstdlib/srand/

Edit:: corrected typo
Last edited on
It appears you forgot to set the random number seed. At the beginning of your main function, try including srand(time(NULL));, before you declare your variables. Make sure not to put this command in a loop or a function, and that it is only executed once, or your numbers will no longer be random.
Topic archived. No new replies allowed.