Thread: Gen. Rand. numb.s

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Two things- first, for srand (time(NULL)) I need #include <ctime> right
    Right. I just tried it with my borland compiler, and interestingly it compiled without that header, but I think it's better to include it for completeness.
    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
    To say the least.
    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)
    I would recommend using a string. For example:
    Code:
    string enemy_name = "Dragon";
    .
    .
    cout << enemy_name << " attacks!" << endl;

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Code:
    //New Problems now, Fortunately, there are only two left!
    cin >> Problems;
    switch (Problems)
    {
    case 1:
    cout << "my first problem now is that I nor my enemy can hit each other, and the program just goes on and on ( at least we fixed the end early problem)" << endl;
    break;
    case 2:
    cout <<  "my second problem is that I am unable to seed the first number, and it remains three each time." << endl;
    break;
    }
    cout << " Please help!!!! \nThanks\nDavid" << endl;
    /* I'm hoping you will understand this, feel free to correct any errors  :D */
    Last edited by Dkunsberg; 06-29-2005 at 09:07 PM.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Quote Originally Posted by swoopy
    >Two things- first, for srand (time(NULL)) I need #include <ctime> right
    Right. I just tried it with my borland compiler, and interestingly it compiled without that header, but I think it's better to include it for completeness.
    To say the least.

    I would recommend using a string. For example:
    Code:
    string enemy_name = "Dragon";
    .
    .
    cout << enemy_name << " attacks!" << endl;
    Great Thanks!!

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >my first problem now is that I nor my enemy can hit each other, and the program just goes on and on
    The best way to debug this is to print out the values for each variable to determine what's happening. Or you can use a debugger. Or you can post your complete code. Otherwise it's a guessing game.

    >my second problem is that I am unable to seed the first number, and it remains three each time.
    Make sure that:
    Code:
    srand(time(NULL));
    is called only once, at the beginning of main().

  5. #20
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by Dkunsberg
    New Problems now, Fortunately, there are only two left!
    Maybe ">>"
    Code:
    cin << problems;
    Spelling?
    Code:
    switch (Problems)

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could have just read the FAQ on random number generation...


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    first of all, all of what you guys said I either know or have tried. First, I did display my attack and I did get random numbers between 1-20, as I expected, but no matter how high I hit I always missed. Second, that cin thing was a typo and I know, third, I have tried the FAQ and the tutorial but neither of them seem to work when I convert it into my program.
    I am still trying to get my code, it will still be on schedule: ASAP.

    Thanks!
    David!

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    I think that I have found the source of my problems, if you look on the previous page at the source code I posted, you can see that I wanted to generate a random number between 0-20+ my attack bonus. unfortunately, when I try this, no matter how high I set my AB, the random number is not affected and still cannot beat my enemy's AC no matter how low I set it. It always seems that it takes the second choice, the one where I miss. originally, this worked, but now, after I changed the way the number was generated for the dmg, it no longer lets me hit, How should I resolve this?

    Thanks for everting so far!
    David
    Last edited by Dkunsberg; 06-30-2005 at 02:35 PM.

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >no matter how high I set my AB, the random number is not affected and still cannot beat my enemy's AC no matter how low I set it
    That's because you're dividing by the attack bonus, not adding it.
    Code:
    strike = (int) N * rand() / (RAND_MAX + attack);
    If you look at one of my previous posts, I pointed this out in your other equation. If you want the attack bonus added to the random number between 0-20, then you want:
    Code:
    strike = N * rand() / RAND_MAX + attack;
    Or you could also use:
    Code:
    strike = rand()%N + attack;

  10. #25
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Quote Originally Posted by swoopy
    >no matter how high I set my AB, the random number is not affected and still cannot beat my enemy's AC no matter how low I set it
    That's because you're dividing by the attack bonus, not adding it.
    Code:
    strike = (int) N * rand() / (RAND_MAX + attack);
    If you look at one of my previous posts, I pointed this out in your other equation. If you want the attack bonus added to the random number between 0-20, then you want:
    Code:
    strike = N * rand() / RAND_MAX + attack;
    Or you could also use:
    Code:
    strike = rand()%N + attack;
    Okay right now I have:
    Code:
    const int N = 21;
    strike=rand()%N+attack;
    cout << strike<< endl;
    cin.get();
    if (strike>enemyac)
    {
    //etc not the problem now
    }
    if (strike<enemyac)
    cout << "You attempt to strike the opposition, but fail to damage your enemy.";
    thats it for that.
    What is happening
    BTW, I am recieving no errors so if you find something that won't work it is probably a typo.
    Last edited by Dkunsberg; 06-30-2005 at 03:02 PM.
    If at first you don't succeed,
    Skydiving is not for you.

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >strike=rand()%N= attack;
    I hope the second = is a typo.

    strike will be some number between 0 and N-1 (20), not counting attack. So if enemyac is less than N-1 (20), at some point the following will become true:
    Code:
    if (strike>enemyac)

  12. #27
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Quote Originally Posted by swoopy
    >strike=rand()%N= attack;
    I hope the second = is a typo.

    strike will be some number between 0 and N-1 (20), not counting attack. So if enemyac is less than N-1 (20), at some point the following will become true:
    Code:
    if (strike>enemyac)

    Yes! but it doesn't, and that is why I am so ****ing messed up!
    This has been the problem, and as you can see I edited it while you were typing up your message. I don't get why! There is no possible reason why this would happen, is it something I previously did within the code, how would that affect what I am doing.
    I think I can get the code up real soon
    David
    If at first you don't succeed,
    Skydiving is not for you.

  13. #28
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I would recommend making a simple program to print out values of rand().
    Code:
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    const int N = 21;
    
    int main()
    {
       srand(time(NULL));
    
       for (int i=0; i<100; ++i)
       {
          cout << rand() % N << " ";
          if ((i+1)%10 == 0)
             cout << endl;
       }
       return 0;
    }
    That should give you values between 0 and 20. If that works, then you take it a step further. Throw it in a function with the attack bonus, then print that. Take it step by step.

    Also make sure you include <cstdlib> for the rand() function.

  14. #29
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Finally! got it!
    here it is (for what its worth)
    please see if this helps you.
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream> 
    
    
    using namespace std;
    void levelup (int level, int spec, int Select, int Dexterity=0, int Strength=0, int Wisdom=0, int Charisma=0, int Intelligence=0,int Constitution=0, int lvlselect=0, int magic=0, int sword=0, int bow=0, int reflex=0, int will=0);
    void Fight (int btd, int estrike, int enemytd ,int tdr, int tdh, int tdl, int enemyhp,int hp,int attack,int ac,int enemyattack, int enemyac, int enemydmg, int dmg, int strike);
    
    int main()
    {
    	char name[256];
    		int btd, enemyhp, tdl, tdh, tdr, estrike, enemytd, hp, attack, ac , enemyattack, enemyac, enemydmg, dmg, Select=0, spec=1, strike=0, Dexterity=0, Strength=0, Wisdom=0, Charisma=0, Intelligence=0, Constitution=0, lvlselect=0, level=0, magic=0, sword=0, bow=0, reflex=0, will=0;
    	srand(time(NULL));
    	cout << "So, what's your name?" << endl;
    	cin.getline ( name,256, '\n' );
    	cout << "Your name is " << name << ", right?" << endl;
    	cout << "Awesome! " << name << ", now we can start!" << endl;
    	cout << "it is now time to choose your skills (to level-up press enter)" << endl;
    	cin.get();
    	level=0; lvlselect=1;
    	levelup ( level, Select, Dexterity, Strength, Wisdom, Charisma, Intelligence, Constitution, 
    		lvlselect, magic, sword, bow, reflex, will, spec); cin.get();
    	btd=1; enemytd=4; tdl=1; tdh=10; tdr=(tdh-tdl)+1; enemyhp=70; hp=70; attack=200; ac=10; enemyattack=2; enemyac=10;
    	Fight ( btd, estrike, enemytd, tdr,  tdl, tdh, enemyhp, hp, attack, ac, enemyattack, enemyac, enemydmg , dmg, strike);
    	return 0;
    	 
    
    }
    
    void levelup (int level, int Select,  int Dexterity, int Strength,
    			  int Wisdom, int Charisma, int Intelligence, int Constitution,
    			  int lvlselect,int magic,int sword,int bow,int reflex,int will, int spec)
    {
    	cout << endl <<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
    	cout << "Level Up!" << endl;
    	cout << "Welcome to the level up screen! You are now level: "<< level << endl;
    	cout << "As you have just leveled up, you are able to increase one basic skill, and gain training in one specific skill.\n";
    	cout <<  lvlselect << " point(s) remaining in basic skills:" << endl;
    	cout << "1. Increase your Strength."<< endl;
    	cout << "2. Increase your Dexterity." <<endl;
    	cout << "3. Increase your Constitution.\n";
    	cout << "4. Increase your Intelligence.\n";
    	cout << "5. Increase your Wisdom.\n";
    	cout << "6. Increase your Charisma.\n";
    	cin>>Select; 
    	switch ( Select ) {
    	case 1:
    		cout << "You have improved your strength"; Strength++;
    		--lvlselect;
    	if (lvlselect>0)
    		levelup ( level, Select, Dexterity, Strength, Wisdom, Charisma, Intelligence, Constitution, lvlselect, magic, sword, bow, reflex, will);
    		break;
    	case 2:
    		cout << "you have improved your dexterity"; Dexterity++;
    		--lvlselect;
    	if (lvlselect>0)
    		levelup ( level, Select, Dexterity, Strength, Wisdom, Charisma, Intelligence, Constitution, lvlselect,magic,sword,bow,reflex,will);
    		break;
    	case 3:
    		cout << "You have improved your constitution"; Constitution++;
    		--lvlselect;
    	if (lvlselect>0)
    		levelup ( level, Select, Dexterity, Strength, Wisdom, Charisma, Intelligence, Constitution, lvlselect,magic,sword,bow,reflex,will);
    		break;
    	case 4:
    		cout << "You have improved your intelligence"; Intelligence++;
    		--lvlselect;
    	if (lvlselect>0)
    		levelup ( level, Select, Dexterity, Strength, Wisdom, Charisma, Intelligence, Constitution, lvlselect,magic,sword,bow,reflex,will);
    		break;
    	case 5:
    		cout << "You have improved your wisdom"; Wisdom++;
    		--lvlselect;
    	if (lvlselect>0)
    		levelup ( level, Select, Dexterity, Strength, Wisdom, Charisma, Intelligence, Constitution, lvlselect,magic,sword,bow,reflex,will);
    		break;
    	case 6:
    		cout << "you have improved your charisma"; Charisma++;
    		break;
    	default:
    		cout<<"Haha, very witty, no skills for you lameo";
    		--lvlselect;
    	if (lvlselect>0)
    		levelup ( level, Select, Dexterity, Strength, Wisdom, Charisma, Intelligence, Constitution, lvlselect,magic,sword,bow,reflex,will);
    		break;
    	}
    	cout << "\nYou are now allowed to train in 1 specific skill:\n";
    	cout << "1. Train in Magic\n";
    	cout << "2. Train in Swords\n";
    	cout << "3. Train in Bows\n";
    	if (level>0)
    	cout << "4. Train in Will\n";
    	if (level>0)
    	cout << "5. Train in Reflex\n";
    	cin >> spec;
    	switch ( spec )
    	{
    	case 1:
    		cout << " You practice with magic and get better at it"; magic++;
    		break;
    	case 2:
    		cout << "You train in swords\n"; sword++;
    		break;
    	case 3:
    		cout << "Your skill with bows has gotten better\n"; bow++;
    		break;
    	case 4:
    		cout << "Your mental willpower has increased\n"; will++;
    		break;
    	case 5:
    		cout << "Your instant reflexes have been improved\n"; reflex++;
    		break;
    	default:
    		cout << "No skills for you now, real funny!";
    		break;
    	}
    
    		cout << "\nYour skills are currently as follows:\n";
    		cout << "Strength: " << Strength << "\n";
    		cout << "Dexterity: " << Dexterity << "\n";
    		cout << "Constitution: " << Constitution << "\n";
    		cout << "Intelligence: " << Intelligence << "\n";
    		cout << "Wisdom: " << Wisdom << "\n";
    		cout << "Charisma: " << Charisma << "\n";
    		cout << "Magic: " << magic << "\n";
    		cout << "Swordsmanship: " << sword << "\n";
    		cout << "Bowmanship: " << bow << "\n";
    		cout << "Will: " << will << "\n";
    		cout << "Reflexes: " << reflex << "\n";
    }
    
    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 tdr, int tdl, int tdh, int enemytd)
    {
    	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=rand()%N + attack;
    	cout << strike << endl;
    	cin.get();
    	if (strike>enemyac)
    	{
    		dmg= tdl+int(tdr*rand()/(RAND_MAX+btd));
    		cout << "A hit!\n";
    		cout << "you do " << dmg << "damage.\n";
    		enemyhp=enemyhp - dmg;
    			cout << enemyhp << hp;
    		cin.get();
    	}
    	if (strike<enemyac)
    		cout << "you attempt to strike the opposition, but fail to damage your enemy." << endl;
    	cout << "Your enemy attacks you: ";
    		strike=rand()*21/ (RAND_MAX + 200);
    	cout << estrike << endl;
    	cin.get();
    	if (estrike>ac)
    	{
    		enemydmg = (int) enemytd * rand()/(RAND_MAX +1.0);
    		cout << "A hit!\n";
    		cout << "your enemy does " << enemydmg << "damage.\n";
    		hp=hp-enemydmg;
    		cin.get();
    	}
    	else
    		cout << "Your enemy misses you." << endl;
    	}
    	if (enemyhp==0)
    		cout << "You strike down your enemy!" << endl;
    	if (hp==0)
    	{
    		cout << "Your enemy strikes at you, and you fall to the ground and die. Sorry! Nice try!" << endl;
    	}
    }
    There it is,
    please refer to see if you find any problems
    Thanks
    David
    If at first you don't succeed,
    Skydiving is not for you.

  15. #30
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Your parameters don't seem to match:
    Code:
    Fight ( btd, estrike, enemytd, tdr,  tdl, tdh, enemyhp, hp, attack, ac, enemyattack, enemyac, enemydmg , dmg, strike);
    .
    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 tdr, int tdl, int tdh, int enemytd)
    For example, in Fight(), enemyac is the same as hp in the function call (the 8th parameter), which is 70. Maybe that's what you want, but seems confusing to me.
    Last edited by swoopy; 06-30-2005 at 08:30 PM.

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, 10: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, 05: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