Changing random #'s

This is a discussion on Changing random #'s within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a text RPG and I want weapons to do a range of damage. For example, a ...

  1. #1
    napkin
    Guest

    Exclamation 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. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You only call srand once to seed the random number generator. You need to call rand() to get a random number.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Thanks, but how do you tell rand() to generate a number up to 4?

    napKIN

  4. #4
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    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. #5
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    heh, good idea, I'll do that when I get home (I'm at school).

    Thanks alot,

    napKIN

  6. #6
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    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. #7
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310

    Thanks!

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

    napKIN

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  3. Changing random numbers
    By napkin111 in forum C++ Programming
    Replies: 1
    Last Post: 09-04-2002, 11:40 AM
  4. Changing Random Numbers?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2002, 12:13 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

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