# random between x and y

• 07-25-2002
harry_p
random between x and y
I know I'll probably get yelled at for asking this because it's been asked a bazillion and one times, but I couldn't find what I needed. You see, for "Mists of Avalon" I have some monster info. It's not getting the monster that's difficult. It's just that the most basic monsters you need to fight at the beginning of the game are getting outrageous HP's like in the 70s when they should be <= 10. I have it declared like this:

monstHp = 3+rand()%10; // between 3 and 10

But it gives me numbers that are, as I said, in the 70's. Sometimes not, usually it's okay, but not all the time, and I need this problem fixed. Any help would be appreciated.

Brendan
• 07-25-2002
Cshot
Quote:

monstHp = 3+rand()%10; // between 3 and 10
This does not give you values between 3 and 10.
rand()%10 gives you a value between 0 to 9....add 3... and you get values between 3 and 12.

Perhaps you're modifying monstHp somewhere else in your code. Step through it with your debugger and see where it's being modified.

Btw, if you want values between 3 and 10 try
monstHp = 3 + rand() % 8;
• 07-25-2002
DISGUISED
or.........

int randomize (int a, int b)
{
return a + rand() % (b - a + 1);
}

and in main

srand (unsigned( time (NULL)));
monsthp = randomize(3, 10);
• 07-25-2002
harry_p
Thanks you both. That helped immensley!!! :)

Brendan
• 07-25-2002
Klinerr1
i was just wondering cause im havig trouble with similiar problems in my rpg (no im not gonna have a random hp and copy ur idea) but would this giveve u a number between 3 and 10?

variable = 3+rand()%7;