invalid min and max arguments for uniform_int

Im having some trouble and im unsure whats going on. So in my code i have attacks that an enemy can use, and I had it so that in main the attacks were added to a vector local to main, well i decided to change that so that a function in the character class now adds attacks to each enemy individually for greater control over what enemy has what attack and now i get this error:

"invalid min and max arguments for uniform_int"

EDIT: So I further was able to find that the issue lies with: randomEnemy.GetAttackList().size() in the code but I dont know why.

I stepped through it with the debugger and its happening at this block of code:

1
2
3
4
5
6
7
8
else
                {
                    cout << randomEnemy.GetName() << " uses " << randomEnemy.GetAttackList()[RandomNumber(generator, 0, randomEnemy.GetAttackList().size() - 1)].GetName();
                    cout << " against " << Hero.GetName() << ", and it does ";
                    cout << randomEnemy.GetAttackList()[RandomNumber(generator, 0, randomEnemy.GetAttackList().size() - 1)].GetPower() << " damage!\n" << endl;

					Hero.TakeDamage(randomEnemy.GetAttackList()[RandomNumber(generator, 0, randomEnemy.GetAttackList().size() - 1)].GetPower());
                }


The code goes to the function I have in main which is this, and thats where it seems to fail:

1
2
3
4
5
6
int RandomNumber(default_random_engine generator, int first, int second)
{
    uniform_int_distribution<int> randomNum(first, second);

    return randomNum(generator);
}


The entire main function is too big to post here so i posted it here: https://justpaste.it/5hh8m
Last edited on
Can GetAttackList().size() ever be 0?
Are you asking because you don't have enough information from my post or are you trying to give me a hint?

I think it can be 0, im pretty sure, since this line:

randomEnemy.GetAttackList()[RandomNumber(generator, 0, randomEnemy.GetAttackList().size() - 1)].GetName()

should do:

Get a random enemy, then get the attack list and choose a random attack by choosing a random number between 0 and the size of the randomly chosen enemies attack list(about 2 or 3), so : RandomNumber(generator, 0, randomEnemy.GetAttackList().size() - 1 should equate to a random number that's not bigger than the random chosen enemies attack list size. However i have no idea why it wont work. I see in my code that an attack can miss, but I added error handling to the code above and it just keeps saying the vector is empty.

I tested to see if the function that sets each enemies attacks was working correctly and it was, each enemy had their correct attacks i set for them and the size was correct, however I believe the issue may lie here at the top of my while loop in main, I do this:

Enemy randomEnemy{ enemyContainer[RandomNumber(generator, 0, enemyContainer.size() -1)] };

When I try to get this enemies attack list size, its always 0. This is the only way that I figured out how to get a random enemy, but I believe its trying to get the attack list of randomEnemy, which is not a valid enemy, just a temporary holder to get random enemy names, so therefore since its not a valid enemy and has no attacks assigned to it, its always 0. So I don't know what to do next.

EDIT: So i replaced the above code with:

1
2
3
 int randomEnemySelection = RandomNumber(generator, 0, enemyContainer.size() - 1);

        enemyContainer[randomEnemySelection];


and it still doesnt work so my theory of it getting a different enemy objects attacks i guess wasnt right, so now im completely confused
Last edited on
Here is a snippet of my while loop up to the problem area:

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
while (choice != -1)
    {
        choice = 0;

        //Choose a random enemy and set it inside vector, if we dont do this
        //A random enemy will be chosen for each call in the fight, we want 
        //the same eney for the duration of the fight.
        Enemy randomEnemy{ enemyContainer[RandomNumber(generator, 0, enemyContainer.size() -1)] };

        Item randomItem{ itemList[RandomNumber(generator, 0, itemList.size() -1)] };
   
        //Randomly generate a reward and set it for this fight, then re-randomize it
        //for the next fight.
        randomEnemy.GiveMoney(RandomNumber(generator, 10, 100));
        randomEnemy.XpToGive(RandomNumber(generator, 10, 60));

        //This makes it so most attacks have a 90% chance to hit.

        cout << "Monster Fight Version 0.20.1-W.3 - 937 Lines of Code\n" << endl;
        cout << "What would you like to do?\n" << endl;

        cout << "1) Fight" << endl;
        cout << "2) Quit" << endl;

        cout << "\n> ";
        cin >> choice;

        switch (choice)
        {
        case 1:
            cin.ignore(numeric_limits<streamsize>::max(), '\n');

            //Initial encounter
            cout << "\n" << Hero.GetName() << " encountered a " << randomEnemy.GetName() << "!" << endl;
            cout << "It has " << randomEnemy.GetHealth() << " Health!\n" << endl;

            turn = 1;

            while (Hero.GetHealth() > 0)
            {
                cout << "\n################################################################" << endl;
                cout << "##" << "                     MONSTER FIGHT                          ##" << endl;
                cout << "################################################################" << endl;

                //============================================
                //Player chooses Attack
                //============================================

                int counter{ 1 };
                int attackChoice{ 0 };

                cout << "\nUse what attack?\n" << endl;

				for (auto& i : Hero.GetAttackList())
                {
                    if (Hero.GetAttackList().empty())
                    {
                        cout << Hero.GetName() << "'s attack list is empty!" << endl;
                    }
                    else
                    {
						cout << counter++ << ") " << i << endl;
                    }
                }
                cin >> attackChoice;

                //Call generator to re-randomize
				generator();

                if(RandomNumber(generator, 0, attackHitChance) == 0)
                {
                    cout << Hero.GetName() << "'s attack missed!\n" << endl;
                }
                else
                {
					cout << "\nACTION------------------------------------------------------------------" << endl;
					cout << Hero.GetName() << " used " << Hero.GetAttackList()[attackChoice -1].GetName() 
                         << " against the " << randomEnemy.GetName() << ", it does " << Hero.GetAttackList()[attackChoice -1].GetPower() << " damage." << endl;
					randomEnemy.TakeDamage(Hero.GetAttackList()[attackChoice -1].GetPower());
                }

				//============================================
                //Check to see if enemy is dead and if so
                //then give player money, xp, and items
                //============================================

                if (randomEnemy.GetHealth() <= 0)
                {
                    cout << Hero.GetName() << " defeated " << randomEnemy.GetName();
                    cout << " and got " << randomEnemy.GetMoney() << " gold and ";

                    Hero.GiveExperience(randomEnemy.GetXpToGive());

                    cout << randomEnemy.GetXpToGive() <<  " experience.\n" << endl;

                    Hero.LevelUp();
                    Hero.GiveMoney(randomEnemy.GetMoney());

                    cout << randomEnemy.GetName() << " dropped " << RandomNumber(generator, 1, 3) << " " << randomItem.GetName() << "'s." << endl;
                    Hero.AddItemToInventory(randomItem, RandomNumber(generator, 1, 3));

                    battles++;
                    Hero.IncrememntKillCounter();
                    cout << "\n================================================================\n" << endl;
                    break;
                }

				//============================================
                //Randomly Choose enemy Attack
                //============================================

                /*ISSUE
                * Something im wondering, if i call generator to randomize attack hit chance
                * will it randomize the enemy name and attack and attack power? it doesnt
                * seem to, it seems to choose one enemy and attack power and attack and
                * use it, however i am unsure.
                */
				generator();

                if(RandomNumber(generator, 0, attackHitChance) == 0)
                {
                    cout << randomEnemy.GetName() << "'s attack missed!" << endl;
                }
                else
                {
                    /*BUG
                    * Having an issue here specifically with randomEnemy.GetAttackList().size()
                    * causing an issue with the randomizer.
                    */
                    cout << "ENEMY ATTACK LIST SIZE: " << randomEnemy.GetAttackList().size() << endl;//Debug
					cout << "HERO ATTACK LIST SIZE: " << Hero.GetAttackList().size() << endl;//Debug

                    if (randomEnemy.GetAttackList().size() == 0)
                    {
                        cout << "ERROR: Enemy attack list vector is empty." << endl;
                    }
                    else
                    {
						cout << randomEnemy.GetName() << " uses " << randomEnemy.GetAttackList()[RandomNumber(generator, 0, randomEnemy.GetAttackList().size() - 1)].GetName();
                        cout << " against " << Hero.GetName() << ", and it does ";
                        cout << randomEnemy.GetAttackList()[RandomNumber(generator, 0, randomEnemy.GetAttackList().size() - 1)].GetPower() << " damage!\n" << endl;

					    Hero.TakeDamage(randomEnemy.GetAttackList()[RandomNumber(generator, 0, randomEnemy.GetAttackList().size() - 1)].GetPower());
                    }
                    
                }
I got it working, I had my enemy vector set before i set the attacks so it would add enemies to the vector but the set attacks wouldnt be added since it was set after.
Topic archived. No new replies allowed.