访问冲突?
我想写一个简单的蒙特卡罗模拟程序。确切地说,我想分析战斗结果,这取决于进攻和防守时不同的部队规模 - 有些是这样的:http://en.wikipedia.org/wiki/Risk_(game)#Dice_probabilities访问冲突?
现在...风险II同一时间规则提供了不同类型的挑战:变化军队规模意味着不同的骰子颜色(这意味着数字的不同分布函数)总之,你的军队规模越小,你最终会以1秒结束,而你的军队规模越大,您越有可能以较高的卷数结束。
由于使用if语句的所有可能条件是最好的巨大的愚蠢,我列出了所有可能的滚动结果在5x12阵列。 (所有骰子12面,5个不同强度,所以你得到5×12)
我想为每个进攻/防守组合进行10000次模拟,但是一旦我意识到这将意味着超过900万的计算,我决定每100个组合减少100个。
以下是代码;一旦我运行它,它会给我访问冲突错误。我不知道我在哪里犯了一个错误。如果有任何建议可以提供,我也会很感激。提前致谢。
/* Risk II Combat Result Table
For starter, we shall consider one-direction attack in RISK II
and generate the combat table for use.
Machine: Pentium Dual Core E6600
Ram: 6G
OS: Windows 7
Compiler: Visual Studio 2010
Jimmy Smith, 24-March-2012
*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
/* Initializing:
Range legend:
White = 1 ~ 6
Yellow = 7 ~ 12
Orange = 13 ~ 20
Red = 21 ~ 30
Black = 31 ~
First row of Dice array corresponds to white dice, and so on.
*/
int Dice[5][12] = { {1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6},
{1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6},
{1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6},
{1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6},
{1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6} };
int Roll_Index [30]= {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4};
int main() {
float Table[30][30];
for (int i = 0; i < 30; i ++)
for (int j = 0; j < 30; j ++)
Table [i][j] = 0.0;
int Result[100];
for (int i = 0; i < 100; i++) Result[i] = 0;
float prob = 0.0;
int Atk = 0;
int Def = 0; //Number of attackers and defenders
int A_Ind = 0;
int D_Ind = 0; //Dice side index
int A_Roll_Index = 0;
int D_Roll_Index = 0; //Roll index on both sides
int A_Dice = 0;
int D_Dice = 0; //Roll result
int Damage = 0;
int Sum = 0; //Internal sum
FILE* fp;
//Time for hard core Monte-Carlo shit! 100 simulation for each situation
for (Atk = 0; Atk<30; Atk++) {
for (Def = 0; Def < 30; Def++) {
for (int i = 0; i < 100; i++) {
int Attacker = Atk +1;
int Defender = Def +1;
while((Attacker>0)&&(Defender>0)) {
A_Ind = (int)(rand()*12);
D_Ind = (int)(rand()*12); //The die is cast!
A_Roll_Index = Roll_Index[Attacker-1];
D_Roll_Index = Roll_Index[Defender-1];
A_Dice = Dice[A_Roll_Index][A_Ind];
D_Dice = Dice[D_Roll_Index][D_Ind];
Damage = min(A_Roll_Index, D_Roll_Index) + 1;
if (A_Dice >= D_Dice) {
Defender -= Damage;
if (Defender == 0) Result[i] = 1;
}
else {
Attacker -= Damage;
if (Attacker == 0) Result[i] = 0;
}
}
}
for (int i = 0; i < 100; i++) Sum+=Result[i];
prob = (float)(Sum/100);
Table[Atk][Def] = prob;
}
}
/* open new file for output and write a title */
fp = fopen("Combat.dat", "w+");
if(NULL == fp) {
printf("cannot open file\n");
return(0);
}
for (Atk = 0; Atk < 30; Atk++){
for (Def = 0; Def < 30; Def++)
fprintf(fp, "%16.8f", Table[Atk][Def]);
fprintf (fp, "\n");
}
fclose(fp);
return(EXIT_SUCCESS);
}
您的代码
A_Ind = (int)(rand()*12);
D_Ind = (int)(rand()*12);
表明您似乎认为rand()
在区间[0,1),这是不是这种情况返回一个数字。相反,INT返回整数范围[0, RAND_MAX]
,所以你需要的东西,如:
A_Ind = rand() * 12.0/RAND_MAX;
如果你的模拟必须是精确的统计,你可能从<random>
库中随机数生成器更好:
#include <random>
typedef std::mt19937 rng_type;
std::uniform_int_distribution<std::size_t> udist(0, 11);
rng_type rng;
// ...
// seed rng first:
rng.seed(some_seed_value);
// roll dice
std_size_t A_Ind = udist(rng), D_Ind = udist(rng);
真是一个惊喜。 A_Ind毕竟是罪魁祸首。谢谢您的帮助。 *我没想到真的* – 2012-03-24 12:18:10
你应该在调试器中运行它。这将确定哪一行代码导致错误。 – 2012-03-24 11:43:07
int结果[100]; for(int i = 0; i inf 2012-03-24 11:44:00
现在这里是一些真正的核心蒙特卡洛狗屎。 @Bamboon,现在你已经破坏了所有的调试乐趣。 :( – bzlm 2012-03-24 11:44:21