兰特数倾斜。 (C ++)

时间:2011-10-19 17:58:04

标签: c++ random numbers

  

可能重复:
  Problems with srand(), C++

基本上,我生成的随机数在大小上都是倾斜的,每次在我的while循环中使用它。它是随机的,不应该倾斜而不是随机的。 变量命中由随机数定义。但正如我所说,随机数不是随机的。此外,随着随机数变大,它超过了我为它设置的限制。

//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
        }
    }

3 个答案:

答案 0 :(得分:4)

程序启动时

hit随机化 如果怪物是兽人,则hit被重新随机化 每当你用斧头附着时,击中永久性增加4点。

您可能希望每次用户攻击时重新随机化hit

答案 1 :(得分:3)

此代码导致此行为:

hit = (hit + 4);

你总是将命中率提高4.所以你应该使用一个临时变量。

答案 2 :(得分:0)

你只需要在程序开始时调用一次srand。然后将随机数生成器“播种”为无限数量的rand()调用 - 多次调用它将使随机数不是随机的,就像你遇到的那样。