# How can I randomly choose a monster from an array of monsters?

I have a problem dealing of how can I call the values inside the arrays correctly. First of all, I am into making an array of monsters and a set of hp for them. I made it this way:

string monsterlist [] = {"Imp", "Orc", "Troll", "Gnome"};
float monsterhealth [] = {5.0f, 8.0f, 9.0f, 10.0f};
int monsterlife;
int monsternumber;


wherein, since i can not call the variables correctly, I made a very simple way to do it, which is:

srand(time(0));
monsternumber = 1 + (rand() % 3);
if (monsternumber == 1)
cout << monsterlist[1];
else if (monsternumber == 2)
cout << monsterlist[2];
else if (monsternumber == 3)
cout << monsterlist[3];
else if (monsternumber == 4)
cout << monsterlist[4];


Is that correct? I know it might be not the proper way. This is what I wanted:

I want to create a random number generation from 1 to 4 and store that variable in monsternumber. The next thing that will happen is, if the monsternumber is equal to 1, for example, should make the monster Imp and 5.0 as its health, seen from string monsterList and monsterHealth.

Is there a proper way to make this simplier or rather the correct way of doing it?

• You may want to look into the struct keyword -- it will not directly solve your problem but it will help you package up your monster names and HP values, et cetera, into a single object you can have a single Monster monsters[] = ... array instead of a bunch of separate arrays you need to keep in sync.
– user1430
Aug 6 '12 at 16:22
• I will research on that ;) Aug 6 '12 at 16:56
• Look up the class keyword instead. It allows you to do very neat OOP things, but essentially it's a struct with functions. Aug 6 '12 at 21:30
• @Bane class and struct is the same in C++ except everything is public by default in a struct, private is default for classes. Everything else including member functions is similar for structs and classes in C++ Aug 6 '12 at 21:49
• @Bane There's no rational reason, just habits, since again, both are similar Aug 6 '12 at 22:51

Your series of if/else statements can be simplified to this:

cout << monsterlist[monsternumber];


However, the line monsternumber= 1+(rand()%3); is problematic in the context of the rest of your program. rand() % 3 produces a value between 0 and 2, which you then add one to. That means monsternumber can be from 1 to 3. It will never be zero and it will never be four.

But arrays in C++ use zero-based indexing. That means the first element in the array is at index 0 -- in order words, monsterlist[0] is the "Imp" string.

Similarly you don't want to try to access monsterlist[4] because you only have four elements in that array, so the only valid indices are 0 through 3. Thus the ideal solution to your problem is something like this:

string monsterlist [] = {"Imp", "Orc", "Troll", "Gnome"};
float monsterhealth [] = {5.0f, 8.0f, 9.0f, 10.0f};
int monsterlife;
int monsternumber = rand() % 4;

cout << monsterlist[monsternumber];

• I guess this helps me, thank you. I tried it and it worked. I can continue with the other parts of the scene I am making in this simple C++ program / game. Thanks so much Josh :) Aug 6 '12 at 16:55
monsternumber = rand()%4;
cout << monsterlist[monsternumber];


That will keep you from having to do a large if ... else sequence.

Also, do rand()%4, or else you will never get a 4 as a result.

Other than that, it looks like it should be doing what you say you want it to do.

• Getting 4 as a result will actually access outside the bounds of the array, which only has four elements.
– user1430
Aug 6 '12 at 16:37
• @Philip: You've forgotten that array indices in C++ start at 0 and go to one less than the number of elements in the array, so adding one to the random number isn't a good idea. Aug 6 '12 at 18:38
• My mistake. I took it out. Aug 6 '12 at 20:53