Dice Statistics with 3 Loops

int main(void)

{
  /*
  **  Probability array.
  **  First number must be >= the number of dice.
  **  Second must be >= dice count times sides.
  */
  int ray[6][40] ;

  int ndice ;
  int target ;
  int sides = 6 ;  /*  This example uses 6 sided dice.  */
  int bumpby ;
  int base ;


  /*  Fill ray with zeroes.  */
  for (ndice = 0 ; ndice <= 6 ; ndice++ )
    for (target = 0 ; target <= 40 ; target++ )
      ray[ndice][target] = 0 ;

  /*  There is one way to roll a zero on zero dice.  */
  ray[0][0] = 1 ;

  /*
  **  Build the array starting with one dice
  **  add one more dice each time around this loop.
  **  In this example, we only roll 3 dice.
  **  The number of dice is determined by the <=
  **  middle statement of the following for.
  */
  for (ndice = 1 ; ndice <= 3 ; ndice++)

    /*
    **  What numbers can one roll with one less dice?
    **  Smallest number is the number of dice.
    **  Largest number is the number of dice times 
    **  the number of sides.
    **  For each number that you could roll with one less dice,
    **  go around the second loop once.
    */
    for (base = ndice - 1 ; base <= (ndice - 1) * sides ; base++)
    {
      /*
      **  How many ways could we roll a base with one less dice?
      **  The answer is already saved in the array.  Pull it out.
      */
      bumpby = ray[ndice - 1][base] ;

      /*  Add bumpby to each slot in way from 1 to sides.  */
      for (target = base + 1 ; target <= base + sides ; target++ )
        ray[ndice][target] += bumpby ;
    }

  /*  Print the results.  */
  for (ndice = 1 ; ndice <= 3 ; ndice++)
    {
      for (target = 1 ; target <= 20 ; target++)
      {
        printf(" %2i",ray[ndice][target]) ;
      }

      printf("\n") ;
    }

  exit(0) ;
}



Dice Statistics Home - Recursive - Java Runtime - Java Statistics Code - Java Applet Code

 


Jump Map Bar

Warp Nine Flying False Colors!

Jump Bar 2