【问题标题】:rand number inclining. (C++) [duplicate]兰特数倾斜。 (C++)[重复]
【发布时间】:2011-10-19 17:58:03
【问题描述】:

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

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

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

【问题讨论】:

  • 要改变的一件事是可能避免多次调用 srand。通常,最好只给随机数生成器播种一次。
  • 请仅以小型可编译方式发布相关代码,以说明问题。
  • 您应该只为随机生成器播种一次,请参阅this 上一个 SO 问题。
  • 你的意思是随机数在增加吗?或者你的意思是随机数应该有倾斜的概率分布?请编辑您的问题以使其清楚。还请发布一个 minimal 工作示例。您发布的代码太长了,而且不是针对问题的。

标签: c++ random numbers


【解决方案1】:

hit 在程序启动时随机分配。
如果怪物是兽人,hit 会重新随机化。
每次你用斧头附着时,命中永久增加四。

您可能还想在每次用户攻击时重新随机化hit

【讨论】:

  • +1 用于识别 OP 询问的问题。
  • 非常感谢它修复了它!
  • 另外,如果怪物是龙呢?应该将hit 随机化吗?
【解决方案2】:

此代码导致此行为:

hit = (hit + 4);

你总是将命中增加 4。所以你应该使用一个临时变量来代替。

【讨论】:

  • +1 用于识别 OP 询问的问题。
【解决方案3】:

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

【讨论】:

  • 好建议,但不是他的问题。
猜你喜欢
  • 2016-12-25
  • 2013-05-25
  • 2011-07-14
  • 1970-01-01
  • 2019-12-15
  • 2017-04-09
  • 2015-06-07
  • 2021-03-27
  • 1970-01-01
相关资源
最近更新 更多