0

Possible Duplicate:
Problems with srand(), C++

Basically, the random number I am generating is inclining in size, everytime it is used inside my while loop. It being random, should't incline and not be random as such. The variable hit is defined by the random number. But as I said, the random number is not random. Also, as the random number gets bigger it goes over the limit I set for it.

//Code im using for random number :
srand((unsigned)time(0));
hit = rand()%15;

//The subject for the problem :
while (1) {
    if (mon1==0) {
        cout << "A dragon appears." << endl;
        system ("pause");
        cout << "Enter 1 to attack and 2 to run away, " <<name << endl;
        cin >> act;
        if (act==2) {
            cout << "You run away. " << endl;
        }
        if (act==1) {
            cout << " You choose to attack! Good luck " << endl;
        }
    }

    if (mon1==1) { // orc
        srand((unsigned)time(0));
        hit = rand()%15;
        ehealth = (100);
        cout << " A orc appears" << endl;
        system ("pause");
        cout << "Enter 1 to attack and 2 to run away, " <<name << endl;
        cin >> act;
        system ("cls");
        if (act==2) {
            cout << "You run away. " << endl;
            system ("cls");
        }
        if (act==1) { // ATTACK HERE MAIN ATTACk TESING !!!!!!!!!!!!!!!!!!!!!!! ORC

            cout << " You choose to attack! Good luck " << endl;
            cout << " Your health is "  << health << endl;
            cout << " The enemys health is " << ehealth << endl;
            cout << " You have 3 abilitys." << endl;
            while (1) {
                cout << " Press 1 to swipe, 2 to block the enemys attack and 3 to use your special attack" <<endl;
                cin >> act1;
                system ("cls");
                if (act1==1) {
                    cout << " You swipe the enemy " << endl; // swipe

                    cout << " You hit for " << hit << endl;
                    if (wep==1) {
                        hit = (hit + 4);
                        cout << " As you are the axe i will add on 4 to that, making " << hit << endl; // axe
                    }
                    ehealth = (ehealth - hit);
                    cout << " The enemys health is " << ehealth << endl;
                    cout << " Your health is " << health << endl;
                    system ("pause");
                }

                if (act1==2) {
                    cout <<" You block, making you immune to the next attack" << endl; //block
                    ehit = (0);
                    //newrand
                    cout << " Your health is " << health << endl;
                    cout << " The enemys health is " << ehealth << endl;
                }
                if (act1==3) {
                    cout << " Special attack is being worked on BUDDY, GUY, FRIEND!! " << endl;
                }
                system ("pause");
                if (health < 0) {
                    cout << "You died, sorry!" << endl;

                }
                if (ehealth < 0) {
                    cout << " Yay! You killed the enemy!" << endl;
                }
                if (health == 0) {
                    cout  << "You died, sorry!" << endl;

                }
                if (ehealth == 0) {
                    cout  << " Yay! You killed the enemy!" << endl;
                }

                cout << " Now it is the enemys turn! " << endl;
                health = ( health - ehit );
                cout << " The enemy hits you for a " << ehit << endl;
                cout << " Your health is now " << health << endl;
                srand((unsigned)time(0));
                ehit = rand()%10;
                system ("pause");
                system ("cls");
            }

            // END OF ORC
        }
    }
Community
  • 1
  • 1
  • 9
    One thing to change is to probably avoid calling srand multiple times. Typically, it is desirable to seed the random number generator only once. – Mark Wilkins Oct 19 '11 at 18:02
  • 2
    please post relevant code only preferably in a small compilable fashion exemplifying the problem. – AJG85 Oct 19 '11 at 18:02
  • 1
    You should only seed the random generator once, see [this](http://stackoverflow.com/questions/7343833/srand-why-call-only-once) previous SO question. – user786653 Oct 19 '11 at 18:02
  • 1
    Do you mean the random number is increasing? Or do you mean the random number should have skewed probability distribution? Please edit your question to make it clear. Also please post a _minimal_ working example. The code you posted is way too long and not specific to the question. – Unapiedra Oct 19 '11 at 18:04

3 Answers3

4

hit is randomized when the program starts.
If the monster is an orc, hit is re-randomized.
Every time you attach with an axe, hit permanently increases by four.

You probably want to re-randomize hit each time the user attacks as well?

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
3

This code causes this behavior:

hit = (hit + 4);

You increase hit always by 4. So you should use a temporary variable instead.

tune2fs
  • 7,605
  • 5
  • 41
  • 57
0

You only need to call srand once, at the start of the program. That "seeds" the random number generator for an unlimited number of rand() calls afterwards - calling it multiple times will just make the random numbers not random, as you encountered.

Colen
  • 13,428
  • 21
  • 78
  • 107