Hey all, I'm creating a text-based RPG and am just now getting to the fighting mechanics. I want to use the rand function to choose a random number within a certain range but am having trouble getting it to execute correctly.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void fight();

int main()
{
fight();
}

void fight()
{
     int ap=10;
     int def=10; 
     int eap=5;
     int edef=5;   
     int dmg;
     int edmg;
     
         system("pause");
     
     dmg=(rand() % ap+1) + (def/edef);    //random number for dmg from 1 to ap 
     edmg=(rand() % ap+1) + (edef/def);  //random number for enemy damage from 1 to eap    
     cout<<"\n\nYou did "<<dmg<<" DAMAGE to the alien"<<endl;
     cout<<"\n the alien did "<<edmg<<" to you"<<endl;
     
     system("pause");
     
return;
     }

As you can see I put variables into my rand function. Is this even allowed? I wasn't sure so I tried anway. I want to have the dmg = a random number from 1 to their attack power(ap), plus the result of their defense divided by their opponents defense. I know something is wrong because when I run this piece it always gives me the same output. To temporarily solve it I could change the rand function to just say 10+1 but then when their attack power changes in the future its not going to be accurate. Thanks for anyone willing to help me out.

This may be helpful. I think the real problem here is you're not seeding rand properly and expecting the default seed to be different each time (which it's not).

thank you narue for the link. it was helpful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.