-1

The intention here is to write a C code for 2 dices rolling simulator. It has to write the answer in a form of histogram (10 results by line). Calculate and give the minimum, maximum, and the average of dice rolls. As well as the duplicates. I have to admit I am stuck right now. Any help would be appreciated.

Here's the code:

   #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100

void initarray(int d[]);
void printarray(int d[]);
void printstar(int n);
void fancyprint(int d[]);
int roll(void);

int main(int argc, char* argv[]) {
{
   int roll(); 
   int d[13]; /* 2-12 hold num of rolls */
   int i;     /* variable de la boucle */
   int starttime = time(NULL); /* temps horloge*/
   srand(1020);

   printf("Simulation de 100 lancees de 2 des");

   for (i = 0; i < N; i++)
      d[roll() + roll()]++;
   printarray(d);
   fancyprint(d);
   printf("Elapsed time: %ld seconds\n",
      time(NULL) - starttime);
   return 0;
}

/* initarray: initialize statistic array */
void initarray(int d[]){
   int i;
   for (i = 2; i < 13; i++)
      d[i] = 0;
}

/* printarray: print each num of rolls */
void printarray(int d[]){
   int i;
   double e[] = {0, 0,
      1.0/36.0, 2.0/36.0, 3.0/36.0, 4.0/36.0,
      5.0/36.0, 6.0/36.0, 5.0/36.0, 4.0/36.0,
      3.0/36.0, 2.0/36.0, 1.0/36.0};
   printf("Sum  Times   Frequency");
   printf("      Exact    Diff\n\n");
   for (i = 2; i < 13; i++)
      printf("%2d %7d %11.7f %10.7f %8.4f\n",
        i,
        d[i],
        (double)d[i]/N*100.0,

        e[i]*100.0,
        ((double)(d[i]) -
           e[i]*N)/N*100.0);
}

/* printstar: print n stars */
void printstar(int n) {
   while (n > 0) {
      printf("*");
      n--;
   }
}


/* fancyprint: print bar graph */
void fancyprint(int d[]){
   int i;
   printf("\n");
   for (i = 2; i < 13; i++) {
      printf("Sum:%3d |", i);

      printstar(300*d[i]/N);

      printf("\n");
   }
   printf("\n");
}

/* roll: simulate rolling a die */
int roll() {
   return (int) (6.0*(rand()/(double)RAND_MAX)
      + 1.0);

return 0;
}
}
mmmtv
  • 1
  • 1

1 Answers1

0

OP's roll() generates a 7 once in a while: when rand() == RAND_MAX.

int roll(void) {
  // return (int) (6.0*(rand()/(double)RAND_MAX) + 1.0);
  return (int) (6.0*(rand()/((double)RAND_MAX + 1) + 1.0);
}

The above will have trouble if (double)RAND_MAX is not exact.

It also still has a small bias.

Consider other approaches that do not use floating point. Such as: https://stackoverflow.com/a/17554531/2410359

Possible other issues.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • The `+1.0` should probably be `+1` after the cast – M.M Nov 24 '15 at 02:32
  • @M.M Could do that, Even better is `((double)(RAND_MAX/2 + 1)*2.0)` as `RAND_MAX` is certainly a power-of-2 - 1` and converting `RAND_MAX/2 + 1` to a `double` will certainly be exact. More so, I was pointing to OP that doing all this without FP is really the best way to go. – chux - Reinstate Monica Nov 24 '15 at 03:36