random between x and y

This is a discussion on random between x and y within the C++ Programming forums, part of the General Programming Boards category; I know I'll probably get yelled at for asking this because it's been asked a bazillion and one times, but ...

  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: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21