Thread: random between x and y

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    94

    Unhappy 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
    Draco dormiens nunquam titillandus

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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;

  3. #3
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    or.........

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

    and in main

    srand (unsigned( time (NULL)));
    monsthp = randomize(3, 10);

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Thanks you both. That helped immensley!!!

    Brendan
    Draco dormiens nunquam titillandus

  5. #5
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    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;
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

Popular pages Recent additions subscribe to a feed