Gen. Rand. numb.s

This is a discussion on Gen. Rand. numb.s within the C++ Programming forums, part of the General Programming Boards category; I need help creating random numbers, as the title tells (sort of). I am really quite new to C++ and ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    22

    Gen. Rand. numb.s

    I need help creating random numbers, as the title tells (sort of). I am really quite new to C++ and would like some answers for making the numbers. My problem is simple. I am making an sort of D&d/NWN/RPG, if you are familliar with this sort of gameplay, I am sure you will understand what I am doing. I actually have my source code on another computer and am currently trying to find the means to transfer it to this computer but many of you might know how to help me without looking at my code:
    My objective is this:
    I want to create a random number that is a varible in which I can change over time within my code. Apparently for me it doen't work. For the game I am creating it is essential that I change the code through the time because the random number stands for my dmg while fighting. (Ex. 1d4, or one "roll" of a dice of four) I need this as my character increases in skill and upgrades his weapons.

    I also have another problem. In the game I have to also make a roll that it one roll of twenty, i must add this to my attack bonus and use this to see if I make a hit. ( If this all seems confusing, it is from many RPG's, WARNING: you will lose your life if you get these games because you will be so addicted, JK) what I do is i must compare this against my enemy's AC (Armor Class) to see if i hit him. This all works for me, but my enemy seemes to be unable to hit me no matter how low my ac is.

    One last problem. when I finally calculate the damage I make (no matter how absurd it may be) I can't find a way to subtract it from my enemy's HP (I do not mean how to display it, just how to acutally subtract the value of the damage from the enemy's HP.

    If anyone has ran into these walls and knows how to beat them please tell a new programmer the answers to these petty questions. If you wish to see my source code, I will try to put it up on the forms, ANY help would be greatly apriciated.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    oh, I just reread the post, if you can't make sense of this without seeing my code I will get it up Asap. Sorry

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Taken from FAQ this should work.


    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <ctime>
    #include <iostream>
    
    int GetRand(int min, int max);
    
    using namespace std;
    
    int main()
    {
      int i, r;
      
      for (i = 0; i < 20; i++)
      {
        r = GetRand(1, 6);//the bounds of the dice
        
        cout<<"Your number is "<<r<<endl;
      }
      int stop;
      cin>>stop;
      
      return 0;
    }
    
    
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        /*
         *  As Init is static, it will remember it's value between
         *  function calls.  We only want srand() run once, so this
         *  is a simple way to ensure that happens.
         */
        srand(time(NULL));
        Init = 1;
      }
    
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      rc = (rand() % (max - min + 1) + min);
      
      return rc;
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Hmm, I'll look at it, though I'm not sure I totally understand it,
    if you have any other suggestions, please tell me, Thanks!

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I have other suggestions but they are even more complicated.

    The mersenne twister is one of my favourites, but unfortunately the example taken from FAQ is about as simple as it gets in c++.

    Invariably, other languages have random number utilities which are more straight forward - matlab for example.

    However, in c++ its more or less up to the user to construct a random number generator which they are satisfied with. In most cases, as in the example I have given, the random numbers generated are just fine.

    However, for more sophisticated methods you have to look else where.

    For more information on pseudo random number generators in c++ go google for answers.

    You will come across the idea of creating a *seed* and other stuff. My advice would be to just use the example and take it at face value. It's already in a function so it should be easy to use.

    When you are more confident perhaps you can start looking to more advanced random number generators.


  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Yeah, apperently thats what I got, because I used a RAND_MAX type way of doing it and the stuff with Srand is the seed. Unfortunately, no matter how much I comnprehended that version of creating numbs it always seemed to have a loophole that I couldn't tell how to fix. (I.E. in my game, when I hit it generates a random numb not between six or four but between the regular randmax
    C++ is fairly confusing (for me ) when it comes up to this, but tanks for your help!

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Post a section of your code perhaps for the board to peruse.

    I'm sure someone will be able to help you.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Quote Originally Posted by treenef
    Post a section of your code perhaps for the board to peruse.

    I'm sure someone will be able to help you.
    Thats just the thing I am trying to do, my **** **** *** *** *** **** *** @)(!)*!ing laptop won't even recognise a CD when its in it, So frustrating!
    i hope to have it up soon..
    thanks

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Quote Originally Posted by Dkunsberg
    Thats just the thing I am trying to do, my **** **** *** *** *** **** *** @)(!)*!ing laptop won't even recognise a CD when its in it, So frustrating!
    i hope to have it up soon..
    thanks
    oh, and how do you tag code?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Ya know what? I'm gonna write down my code, if you need info from other parts of the code, tell me and I'll give it to you.

    Here:

    Code:
    Void Fight (int estrike, int btd, int enemyhp, int hp, int attack, int ac, int enemyattack, int enemyac, int enemydmg, int dmg, int strike, int td, int enemytd)
    /* estrike stands for enemy strike, btd= bonus total damage,       hp=health points, ac=armor class, and td =total possible damage.*/
    {
    cout << "you engage in a fight against the enemy:\n";
    while (enemyhp>0 && hp>0
    {
    cout << "you attack your enemy: "
    const int N = 21;
    strike = (int) N * rand() / (RAND_MAX + attack);
    cout << strike << endl;
    cin.get();
    if (strike>enemyac)
    dmg=(int) td * rand() / (RAND_MAX =btd);
    while (dmg>0)
    {
    dmg--;
    enemyhp--;
    // to basically subtract damage from the enemy's HP.
    }
    /*etc, etc, etc, thats basically it, its the same thing for my enemy except with all different varibles. */
    
    {
    there ya have it, my messy noobish and completely messed up code.
    btw... if there are any typo's by pass them, I can ACTUALLY RUN THIS THING haha, but it doesn't work the way I want it too. , uh uhh opps, i mean (the progression to the state of madness)
    Help me if you can please.
    Thanks! keep postin'!
    Last edited by Dkunsberg; 06-28-2005 at 09:24 AM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > (RAND_MAX =btd);
    Is that = a typo, or is the real code like that?

    Also, are you wanting to add btd to the random number? In that case it would be:
    Code:
    dmg=(int) td * rand() / RAND_MAX + btd;
    Or better would probably be:
    Code:
    dmg=(int) (td * (double) rand() / RAND_MAX + btd);
    >I can't find a way to subtract it from my enemy's HP (I do not mean how to display it, just how to acutally subtract the value of the damage from the enemy's HP.

    Did you try:
    Code:
    enemyhp = enemyhp - dmg;

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    hmm maybe.... I have been thinking, wat if i used the rand()%11 thing to create a function for each weapon I used, thereby being able to change the dice roll as the weapon is upgraded or increased

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Quote Originally Posted by swoopy
    > (RAND_MAX =btd);
    Is that = a typo, or is the real code like that?

    Also, are you wanting to add btd to the random number? In that case it would be:
    Code:
    dmg=(int) td * rand() / RAND_MAX + btd;
    Or better would probably be:
    Code:
    dmg=(int) (td * (double) rand() / RAND_MAX + btd);
    >I can't find a way to subtract it from my enemy's HP (I do not mean how to display it, just how to acutally subtract the value of the damage from the enemy's HP.

    Did you try:
    Code:
    enemyhp = enemyhp - dmg;
    OK, for starters, Thanks, The subtraction thing works well. I have another problem instead. As you see in my "while" loop the fighting should continue until one of us goes to or below Zero, correct, un fortunately, it stops after a few rounds of fighting. In addition it does seem to do anything random, it will generate numbers but they same numbers come up each time, so now I have four problems

    "One step forward, two steps backward."
    But anyway thanks alot for the suggestions, and keep 'em comin....

    Please!


    Thanks
    David

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > it stops after a few rounds of fighting.
    Maybe hp or enemyhp became <= 0. You can cout them to see for yourself.

    >it will generate numbers but they same numbers come up each time
    You need an srand(time(NULL)) at the beginning of main() to seed the random number generator. See this FAQ entry:
    wat if i used the rand()%11 thing to create a function for each weapon I used, thereby being able to change the dice roll as the weapon is upgraded or increased
    That's a very good idea. One thing though. From what I've read:
    Code:
    rand()%11
    is not supposed to be quite as random as:
    Code:
    rand()*11/(RAND_MAX+1)
    But you probably shouldn't worry about that until you get a working game. Then maybe you can tweak the rng.
    Last edited by swoopy; 06-29-2005 at 02:01 PM.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Quote Originally Posted by swoopy
    > it stops after a few rounds of fighting.
    Maybe hp or enemyhp became <= 0. You can cout them to see for yourself.

    >it will generate numbers but they same numbers come up each time
    You need an srand(time(NULL)) at the beginning of main() to seed the random number generator. See this FAQ entry:
    That's a very good idea. One thing though. From what I've read:
    Code:
    rand()%11
    is not supposed to be quite as random as:
    Code:
    rand()*11/(RAND_MAX+1)
    But you probably shouldn't worry about that until you get a working game. Then maybe you can tweak the rng.
    cool! Thanks alot! Two things- first, for srand (time(NULL)) I need #include <ctime> right, if not, what do I use.
    also, no, apperently, with my old random number generater, it seemingly only did really large (Or should I say really small?) negative numbers. so my my enemy would be getting like 7E10 hp each hit. A little unfair don't you think, I have no idea why it would quit off.
    Thanks,
    P.S. How do you name char strings? (I mean not the player, but the programmer so I can include the name of my enemy within the fight seceunce by simply naming a char string before hand)
    David

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 05:44 PM
  2. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  3. 1337 bible, Gen 11
    By Paz_Rax in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-20-2005, 09:40 PM
  4. mission notimpossible (maze gen)
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 04:51 PM
  5. What next Gen system you own now?
    By Golden Bunny in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-11-2002, 09:01 PM

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