Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:58:50+00:00 2026-05-31T22:58:50+00:00

I was trying to write a simple Monte Carlo simulation program. To be exact,

  • 0

I was trying to write a simple Monte Carlo simulation program. To be exact, I wanted to analyze the combat results depending on the varying army sizes on both offense and defense – something in tune of this: http://en.wikipedia.org/wiki/Risk_(game)#Dice_probabilities

Now… Risk II Same time rule offers different kind of challenge: varying army size means different color of dice (which means different distribution function for the numbers) In short, the smaller the size of your army is, the more likely you’ll end up with 1s, while the larger the size of your army is, the more likely you’ll end up with higher number of rolls.

Since using if statements for all the possible condition was a colossal stupidity at its finest, I tabulated all the possible rolling results in 5×12 array. (12 sides in all of the dice, and 5 varying strength, so you get 5×12)

I thought of carrying out 10000 simulations for each offense/defense combinations but once I realized that would mean over 9 million calculations, I decided to cut it short at 100 per combination.

The following is the code; once I run it, it gives me the Access Violation error. I don’t know where I made an error. If there is any advice you could offer, I’d appreciate that too. Thanks in advance.

/* 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);

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:58:52+00:00Added an answer on May 31, 2026 at 10:58 pm

    Your code

    A_Ind = (int)(rand()*12);
    D_Ind = (int)(rand()*12);
    

    indicates that you seem to think that rand() returns a number in the range [0,1), which is not the case. Instead, int returns an integer in the range [0, RAND_MAX], so you need something like:

    A_Ind = rand() * 12.0 / RAND_MAX;
    

    If your simulation has to be statistically accurate, you are probably better off with a random number generator from the <random> library:

    #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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program in VC++ which will just initialize the
I am trying to write a simple multithreaded program in C#. It has a
I am trying to write simple program using ONLY for loop ( if then
Trying to write a simple VCL program for educating purposes (dynamicly created forms, controls
Im trying to write a simple program that takes 5 images and allows you
i'm trying to write simple tcp\ip client-server. here is server code: internal class Program
I'm trying to write a simple maze search program in prolog, before I add
I am trying to write a simple n-body gravity simulation with 4 particles in
Im trying to write a simple sample program which checks for any new mail
I am trying to write a simple C program that creates directories (a mkdir

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.