【问题标题】:How can I get rand to give me proper number using accuracy?我怎样才能让 rand 使用准确度给我正确的数字?
【发布时间】:2014-12-04 18:32:03
【问题描述】:

我有一个实验室,我们必须编写一个程序来模拟决斗,对于 Aaron,A 的概率为 1/3,Bob 的概率为 1/2,Charlie 永远不会错过。程序应使用随机数和问题中给出的概率来确定射手是否击中目标,模拟 10,000 次决斗。

第一次使用 rand 并且我的函数出现了一些问题,我的教授告诉我输入 %accuracy 所以你只需要一个函数,它会从函数调用中输入玩家的准确度,但我收到一个错误: int' anddouble' 到二进制 `operator%' 类型的无效操作数,我很困惑,因为准确度是一个数字。

我也遇到了 while 循环的问题,因为“战士”应该只在玩家被击中时减去一个,而有时玩家没有被击中。

#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>

using namespace std;

double rand1 (double accuracy);
bool aaron_shoot (double a, bool& charlie, bool& bob);
bool bob_shoot(double b, bool& charlie, bool& aaron);
bool charlie_shoot (double c, bool& bob, bool& aaron);

int main() {

 bool aaron, bob, charlie; 
 int aaron_wins = 0, bob_wins = 0, charlie_wins = 0; 
 srand(time(0));

   for ( int battles = 1; battles <= 10000; battles++){

      int fighters = 3; 
      while ( fighters > 1){   
        aaron = aaron_shoot(0.333, charlie, bob);          
        bob = bob_shoot (0.5, charlie, aaron);    
        charlie = charlie_shoot (1.0, bob, aaron); 
         //need an argument to make sure a shooter does not shoot if they are dead, and to count             how many are left if there is a hit
} //keeps track of the win at the end of each round of battles when  
  //there is one player left standing       
       aaron_wins = aaron_wins + aaron;
       bob_wins = bob_wins + bob;         
       charlie_wins = charlie_wins + charlie;          
 }
 cout << "Aaron won " << aaron_wins<< "/10000 duels or " << (aaron_wins/100)<< "%.\n";
 cout << "Bob won " << bob_wins << "/10000 duels or " << (bob_wins/100)<<"%.\n";
 cout << "Charlie won " << charlie_wins << "/10000 duels or " << (charlie_wins/100)<<"%.\n";

system ("Pause"); 
return 0;

}


bool aaron_shoot (double a, bool& charlie, bool& bob){
 if (charlie == true){ //is alive
             if (rand1 (a) >= a){
                       return (charlie = false);                           
                       }
             }
 else if (bob == true){
              if (rand1 (a) >= a){
                        return (bob = false);

                        }
              }          
}

bool bob_shoot (double b, bool& charlie, bool& aaron){
 if (charlie == true){
             if (rand1 (b) >= b){
                       return (charlie = false);

                       }
             }
 else if (aaron == true){
              if (rand1 (b) >= b){
                        return (aaron = false);

                        }
              }      
}   

bool charlie_shoot (double c, bool& bob, bool& aaron){
 if (bob == true){
             if (rand1 (c) >= c){
                      return (bob = false);
                       }
             }
 else if (aaron == true){
              if (rand1 (c) >= c){
                        return (aaron = false);

                        }
              }          
}

double rand1 (double accuracy){
   double r = rand ();            
   return (r / RAND_MAX) < accuracy;

}           

【问题讨论】:

  • 编译器告诉你operator% 不适用于双打。你给它一个双倍。你需要modf。下次发布一个重现问题的简短示例,而不是所有您的代码。
  • @Borgleader 他不需要modf;将rand() 的结果转换为[0.0, 1.0) 范围内的浮点值,并将其与准确性进行比较非常简单。
  • @JamesKanze “需要 modf” 显然我的意思是对 double 类型的参数应用模运算......我不声称知道 OP 试图用这段代码实现什么。跨度>
  • “简单”是指“委员会必须给每个人一个随机库才能正确完成它,因为没有人能正确完成它”?
  • 您(几乎可以肯定)只想调用一次srand(例如,在main 的开头),而不是每次都像这里所做的那样获得另一个随机数。跨度>

标签: c++ function random while-loop srand


【解决方案1】:
double rand1 (double accuracy){
   srand(time(0));
   int a = rand ()% accuracy; <-- THIS
   double b = ((double)a / RAND_MAX);
   return b;
} 

我不确定你认为那条线是做什么的,但它没有任何意义。如果要将精度保持为分数,请使用:

    double r = rand ();
    if ( (r / RAND_MAX) < accuracy)

另外,请勿多次致电srand。否则,在同一秒内对rand1 的两次调用将从rand 获得相同的返回值。

【讨论】:

  • 谢谢,那么函数看起来像这样吗? double rand1(双精度){ double r = rand (); if ( (r / RAND_MAX)
  • 我建议返回一个布尔值,所以return (r / RAND_MAX) &lt; accuracy;
  • 非常感谢。它现在正在工作。我只是不知道如何修复while循环。有什么建议吗?
  • 你只需要修复它。例如,如果战士死了,不要让他们开枪。另外,不要为其他战士创建两个都命名为aaron 的变量,依此类推。
  • 非常感谢您的帮助,我是新手。我认为我在函数中的 if 语句可以确保它们是否被击中,它们不会被击中。我的while循环是看是否有人被击中,如果是,那么战士——;这就是我需要帮助的地方
【解决方案2】:

您不能对 double 使用模数。有几种方法 处理这个,但如果准确度总是 1 超过 smal 整数a,你可以做rand() % a == 0看它是否命中。 否则,应先将rand() 的结果转换为 在[0.0, 1.0) 范围内的double,由rand() / (RAND_MAX + 1.0, 然后如果指定的精度大于那个,它是 错过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    相关资源
    最近更新 更多