Random Number With Limits

So basically I have to create this text based game in which a user fights a computer with three different weapons. The weapons are supposed to generate random numbers within certain ranges in order to determine their damage, but my weapons seem to be hitting for more than the range that I set them. I know that I don't have something that stops the user from using weapons past their round limit yet, but I'll get to that. My main problem is faulty random number generating. Here's 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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    //Choices from the menu and the amount of rounds left
    const int CANNON_CHOICE = 1,
              GRENADE_CHOICE = 2,
              RIFLE_CHOICE = 3;
          int CANNON_ROUNDS = 3,
              GRENADE_ROUNDS = 4,
              COMP_CANNON_ROUNDS = 3,
              COMP_GRENADE_ROUNDS = 4;

    //Getting the system time, seeding the random number
    //generator, and the damages that each weapon does
    unsigned seed = time(0);
    srand(seed);

    int cannon;
    int grenade;
    int rifle;

    //The health of the user and the computer
    int userHealth = 100,
        compHealth = 100;

    //The user's and computer's choice variables
    int userChoice,
        compChoice = 1 + rand() %3;

    //Directions
    cout << "You must do battle with the computer!\nEach player has 100 health as of now, begin!" << endl;

    //Big picture loop, continue turns until one player's health is zero or less
    while(userHealth > 0 && compHealth > 0)
    {
        cout << "1. Cannon with " << CANNON_ROUNDS << " rounds left" << endl
             << "2. Grenade with " << GRENADE_ROUNDS << " rounds left" << endl
             << "3. Rifle" << endl;
        cin >> userChoice;

        switch(userChoice)
        {
            case CANNON_CHOICE:
            {
                int cannon = 10 + rand() %15;
                compHealth = compHealth - cannon;
                CANNON_ROUNDS = CANNON_ROUNDS - 1;
                cout << "The computer's health is now " << compHealth << ".";
                break;
            }
            case GRENADE_CHOICE:
            {
                grenade = 7 + rand() %12;
                compHealth = compHealth - grenade;
                GRENADE_ROUNDS = GRENADE_ROUNDS - 1;
                cout << "The computer's health is now " << compHealth << ".";
                break;
            }
            case RIFLE_CHOICE:
            {
                rifle = 3 + rand() %8;
                compHealth = compHealth - rifle;
                cout << "The computer's health is now " << compHealth << ".";
                break;
            }
        }

        switch(compChoice)
        {
            case CANNON_CHOICE:
            {
                int cannon = 10 + rand() %15;
                userHealth = userHealth - cannon;
                COMP_CANNON_ROUNDS = COMP_CANNON_ROUNDS - 1;
                cout << "The computer chose the cannon!\nYour health is now " << userHealth << endl;
                break;
            }
            case GRENADE_CHOICE:
            {
                grenade = 7 + rand() %12;
                userHealth = userHealth - grenade;
                COMP_GRENADE_ROUNDS = COMP_GRENADE_ROUNDS - 1;
                cout << "The computer chose the grenade!\nYour health is now " << userHealth << endl;
                break;
            }
            case RIFLE_CHOICE:
            {
                rifle = 3 + rand() %8;
                userHealth = userHealth - rifle;
                cout << "The computer chose the rifle!\nYour health is now " << userHealth << endl;
                break;
            }
        }
    }

    //Closing if statement
    if(userHealth > 0)
    {
        cout << "Congratulations!  You have defeated the evil computer!";
    }
    else
    {
        cout << "Better luck next time, the computer has won!";
    }
}
Hi

get Mersenne Twister random number generator C++, you dont have to code your self what other already done very well :-)
Last edited on
What do you mean by "outside the range you have set them"?
10 + rand() %15; This will generate a random number in the range 10-24. Is that what you want?
rand() does not really generate real random numbers, it's implementation is very poor
Three things. One Peter87's reply is the correct way to put random numbers in a range, but I would make it in to a function:
 
int randRange(int randMn,int randMx) { return randMn + rand() % randMx }

instead of using const int i would use enums, and instead of using one use zero for CANNON_CHOICE so it works better with arrays and you don't have to do the range thing:
 
enum choice_t { CANNON_CHOICE ,GRENADE_CHOICE, RIFLE_CHOICE };

how ever if you still want to use 1,2,and 3 then:
 
enum choice_t { CANNON_CHOICE = 1,GRENADE_CHOICE, RIFLE_CHOICE };

the next one will default to two
Last edited on
Topic archived. No new replies allowed.