1. Changing random #'s

I'm trying to make a text RPG and I want weapons to do a range of damage. For example, a short sword would do 2-6 damage. To do this, I want to use 2 variables, W & WV. W is for weapon and WV is for weapon variance (or variability, or some other word starting with v...). This is my basic damage formula for a weapon dealing 2-6 damage:

code:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
int damage();
int w,wv;
w=2
wv=4;

int damage()
{
int dam;
dam=srand(wv)+w;//this is the line I'm not sure of
return dam;
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Thats how I want to do it, generate a number from 0-4, then add 2 to it, thus dealing 2-6 damage. I've gotten something like that to work, its just that the number won't change each time it is called. I want to do a similar thing for thac0, but I'll do that once I know how to make random numbers that change each time they are called.

Thanks,

napKIN

2. You only call srand once to seed the random number generator. You need to call rand() to get a random number.

3. Thanks, but how do you tell rand() to generate a number up to 4?

napKIN

4. Would this work then?

code:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
int damage();
int w,wv;
w=2
wv=4;

int damage()
{
int dam;
dam=rand()%wv+w;
return dam;
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Thanks for the help,

napKIN

5. heh, good idea, I'll do that when I get home (I'm at school).

Thanks alot,

napKIN

6. first, include <ctime> for time function. anywhere in your program, put this line once: srand(TIME(NULL)); . Then here is the formula for random numbers: dam=low + rand() % (high-low+1). so if you wanted between 2 and 6, with w=2, wv=6:
Code:
`dam = w + rand() % (wv - w + 1);`
that will make a new random number each time, between 2 and 6.

7. Thanks!

Thanks alot, I've been stuck on this FOREVER.

napKIN