//===================================================== file = diceSim2.c =====
//=  Simulation of rolling dice and getting a pair                            =
//=============================================================================
//=  Notes:                                                                   =
//=    1) This program is for assignment #2 for CIS 4930 (Simulation) for     =
//=       Summer 2011. This is an example of a Monte Carlo simulation.        =
//=    2) See NUM_ROLL and TRACE in the #define section                       =
//=    3) Ignore two warnings on always false and unreachable code            =
//=---------------------------------------------------------------------------=
//=  Build: bcc32 diceSim2.c, cl diceSim2.c, gcc diceSim2.c                   =
//=---------------------------------------------------------------------------=
//=  Execute: diceSim2                                                        =
//=---------------------------------------------------------------------------=
//=  Sample execution:                                                        =
//=                                                                           =
//=   >>> The simulation is running...                                        =
//=   =============================================================           =
//=   ==  *** Simulation of rolling dice and getting a pair ***  ==           =
//=   =============================================================           =
//=   =  MODEL INPUT:                                                         =
//=   =    Number of dice rolls = 10000000                                    =
//=   =============================================================           =
//=   =  MODEL OUTPUT:                                                        =
//=   =    Percent pairs        = 16.661420 %                                 =
//=   =============================================================           =
//=---------------------------------------------------------------------------=
//=  History: KJC (05/24/11) - Genesis                                        =
//=============================================================================
//----- Include files ---------------------------------------------------------
#include <stdio.h>            // Needed for printf()
#include <stdlib.h>           // Needed for rand()

//----- Constants -------------------------------------------------------------
#define    FALSE       0      // Boolean false
#define     TRUE       1      // Boolean true

#define NUM_ROLL 10000000     // Number of iterations to simulate
#define    TRACE    FALSE     // Number of blocks transferred per iteration

//===== Main program ==========================================================
void main(void)
{
  int    redDie;          // Value of red die (1, 2, ..., 6)
  int    blueDie;         // Value of blue die (1, 2, ..., 6)
  int    pairCount;       // Count of event redDie == blueDie
  int    i;               // Loop counters

  // Output a "running" banner
  printf(">>> The simulation is running... \n");

  // Run the simulation for NUM_ROLL dice rolls
  pairCount = 0;
  for (i=0; i<NUM_ROLL; i++)
  {
    // Roll the two die
    redDie = (rand() % 6) + 1;
    blueDie = (rand() % 6) + 1;

    // Output values of dice is TRACE is enabled
    if (TRACE == TRUE)
      printf("redDie / blueDie = %d / %d \n", redDie, blueDie);

    // Check if a pair and increment pairCount if so
    if (redDie == blueDie)
      pairCount++;
  }

  // Output results
  printf("=============================================================== \n");
  printf("==  *** Simulation of rolling dice and getting a pair ***    == \n");
  printf("=============================================================== \n");
  printf("=  MODEL INPUT:                                                 \n");
  printf("=    Number of dice rolls = %d    \n", NUM_ROLL);
  printf("=============================================================== \n");
  printf("=  MODEL OUTPUT:                                                \n");
  printf("=    Percent pairs        = %f %% \n", 100.0 * pairCount / NUM_ROLL);
  printf("=============================================================== \n");
}

