Ask coding questions

← Back to all posts
Using a random variable in C++
LoveFromSkyy (137)

I'm working on the first level of my game and I want to know how I can make the monster use a random attack on the player AND use the correct number of damage. Here is the data that I wrote for this part (I'm not including the whole code because the rest is useless):

void LevelStart(int XP, int Lvl, int NewXP, int AddXP, int NewLvl){
	int playerAttack;
		struct Hero player;
	struct Attacks playerAttacks;
	struct Attacks monsterAttacks;

	struct Monster easyLevelMonster;

	// Player attacks and stats = 150;
	playerAttacks.attack1 = "FireBall";
	playerAttacks.attack2 = "Fire Slash";
	playerAttacks.attack3 = "Fire Barrage";
	playerAttacks.attack4 = "Blue Flame Bomb";
	playerAttacks.damageAttack1 = 4;
	playerAttacks.damageAttack2 = 9;
	playerAttacks.damageAttack3 = 14;
	playerAttacks.damageAttack4 = 19;

	// Monster attacks and stats
	easyLevelMonster.monsterCount = 1;
	easyLevelMonster.monsterType = "monster";
	easyLevelMonster.monsterHealth = 100;
	monsterAttacks.attack1 = "bash";
	monsterAttacks.attack2 = "Spin attack";
	monsterAttacks.attack3 = "Heavy punch";
	monsterAttacks.damageAttack1 = 10;
	monsterAttacks.damageAttack2 = 15;
	monsterAttacks.damageAttack3 = 20;

Tell me if you need more of the code to answer this. Please help me with this.

Answered by 19wintersp (1143) [earned 5 cycles]
View Answer
19wintersp (1143)

Do you want to pick a random attack? You can generate a random number using rand from cstdlib, and use that to choose. Here's a function which generates a random integer from min to max inclusive:

#include <cstdlib>

int randomInteger(int min, int max) {
  int mod = max - min + 1;
  return (rand() % mod) + min;

int randomInteger(int max) {
  return randomInteger(0, max);
LoveFromSkyy (137)

Thank you! How can I implement this into my code? I looked stuff like this up but I don't want a random number, I'm looking for a random variable like

monsterAttacks.attack1 = "bash";
	monsterAttacks.attack2 = "Spin attack";
	monsterAttacks.attack3 = "Heavy punch";


19wintersp (1143)

@LoveFromSkyy You could store those as an array rather than giving them names, or if you prefer to use these predefined names:

switch(randomInteger(1, 3)) {
case 1:
  //do something with monsterAttacks.attack1
case 2:
  //do something with monsterAttacks.attack2
case 3:
  //do something with monsterAttacks.attack3

Strongly recommend the array though.

LoveFromSkyy (137)

Okay, I'll try, I suck at arrays


19wintersp (1143)

@LoveFromSkyy Here's how I might implement it:

struct Attack {
  char* attack;
  int damage;

class Monster {
  /* other stuff... */
  Attack attacks[3];
  /* other stuff... */

  Attack getAttack() {
    return this.attacks[randomInteger(0, 2)];
Baconman321 (1104)

@19wintersp Doesn't the random integer have to be seeded?
I ran into a problem seeding an integer inside a loop due to it most of the time being seeded with time (which doesn't change fast enough for loops).

19wintersp (1143)

@Baconman321 Not too sure, I think srand does, but using rand seems fine.

Baconman321 (1104)

@19wintersp Ah, yes. I remember now...
Yeah, I was thinking "hmmm, that command doesn't seem like the ones I used".

LoveFromSkyy (137)

I made the Arrays for attacks and attack damage. How do I pick a random attack to be used?

19wintersp (1143)

@LoveFromSkyy Assuming you've added the code from my first comment, you can select a random element from the array:

<array name>[randomInteger(<array length - 1>)]

so for the attacks array, with a length of 3: