Advertisement
Guest User

Untitled

a guest
Jun 6th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int comp(const void *a, const void *b)
  5. {
  6.     return ((*(int*)a == *(int*)b) ? 0 : (*(int*)a < *(int*)b) ? -1 : 1);
  7. }
  8.  
  9. int main(int argc, char **argv)
  10. {
  11.     FILE    *in = fopen(argv[1], "r");
  12.     char    buffer[50];
  13.     float   payprob[2][20], sum = 0, result;
  14.     int     trials = atoi(argv[2]), hands = atoi(argv[3]),
  15.         worst = atoi(argv[4]), i = 0, j, k, total,
  16.         *run = (int *)malloc(hands*sizeof(int)),
  17.         *low = (int *)malloc(trials*sizeof(int)),
  18.         *final = (int *)malloc(trials*sizeof(int));
  19.  
  20.     srand(time(NULL));
  21.  
  22.     while (fgets(buffer,sizeof(buffer),in))
  23.     {
  24.         sscanf(buffer, "%f %f", &payprob[0][i], &payprob[1][i]);
  25.         i++;
  26.     }
  27.  
  28.     total = i;
  29.  
  30.     for(i = 0;i < total; i++)
  31.         sum += payprob[1][i];
  32.  
  33.     payprob[1][0] = sum - payprob[1][0];
  34.  
  35.     for(i = 1; i < total; i++)
  36.         payprob[1][i] = payprob[1][i-1] - payprob[1][i];
  37.  
  38.     for(i = 0; i < trials; i++)
  39.     {
  40.         for (j = 0; j < hands; j++)
  41.         {
  42.             result =  (double)rand() / (double)RAND_MAX;
  43.  
  44.             for (k = 0; k < total; k++)
  45.                 if (result > payprob[1][k])
  46.                 {
  47.                     run[j] = (int)payprob[0][k] - 1;
  48.                     break;
  49.                 }
  50.         }
  51.  
  52.         final[i] = 0;
  53.         low[i] =0;
  54.  
  55.         for(j = 0; j < hands; j++)
  56.         {
  57.             final[i] += run[j];
  58.             if (final[i] < low[i])
  59.                 low[i] = final[i];
  60.         }
  61.     }
  62.  
  63.     qsort(low,trials,sizeof(int),comp);
  64.     qsort(final,trials,sizeof(int),comp);
  65.  
  66.     j = 1;
  67.     k = 1;
  68.  
  69.     for(i = 0; i < trials ; i++)
  70.         if (j && low[i] > worst)
  71.         {
  72.             printf("%d trials out of %d worse than a loss of %d (%f%%)\n",
  73.                 i-1, trials, worst, (float)(i-1)*(float)100/(float)trials);
  74.             j--;
  75.         }
  76.         else if (k && final[i] > worst)
  77.         {
  78.             printf("%d trials out of %d worse than a final result of of %d (%f%%)\n",
  79.             i-1, trials, worst, (float)(i-1)*(float)100/(float)trials);
  80.             k--;
  81.         }
  82.  
  83.     return(0);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement