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()

void fight()
     int ap=10;
     int def=10; 
     int eap=5;
     int edef=5;   
     int dmg;
     int edmg;
     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;

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.

6 Years
Discussion Span
Last Post by crimes

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).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.