training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

dice.c (1119B)


      1 /* Exercise 6.19 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <time.h>
      6 #include "../practice/mt19937ar.c"
      7 
      8 int dice(void);
      9 
     10 #define LAUNCHES 36000
     11 #define LIMIT 50
     12 
     13 int main()
     14 {
     15    int i, t;
     16    int results[13] = { 0 };
     17 
     18    init_genrand( time(NULL ) );
     19    //srand( time(NULL) );
     20 
     21    for(i = 1; i <= LAUNCHES; i++)
     22       ++results[dice()];
     23 
     24    printf("Print the results (* = not sense)\n");
     25    printf("There is a tollerance of \"n [-+] %d\"\n\n", LIMIT);
     26    for(i = 2; i <= 12; i++) {
     27       printf("%d:\t%d", i, results[i]);
     28 
     29       if(i <= 7) {
     30 	 t = LAUNCHES / 36 * (i - 1);
     31 
     32 	 if(results[i] < t - LIMIT || results[i] > t + LIMIT)
     33 	    printf("\t*\tout of range (%d-%d)", t - LIMIT, t + LIMIT);
     34       }
     35       else {
     36 	 t = LAUNCHES / 36 * ((14 - i) - 1);
     37 
     38 	 if(results[i] < t - LIMIT || results[i] > t + LIMIT)
     39 	    printf("\t*\tout of range (%d-%d)", t - LIMIT, t + LIMIT);
     40 
     41       }
     42       printf("\n");
     43    }
     44 
     45    return 0;
     46 } /* E0F main */
     47 
     48 int dice(void)
     49 {
     50    //int d1 = 1 + rand() % 6;
     51    //int d2 = 1 + rand() % 6;
     52    int d1 = 1 + genrand_int32() % 6;
     53    int d2 = 1 + genrand_int32() % 6;
     54    return d1 + d2;
     55 }
     56