Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int comp(const void *a, const void *b)
- {
- return ((*(int*)a == *(int*)b) ? 0 : (*(int*)a < *(int*)b) ? -1 : 1);
- }
- int main(int argc, char **argv)
- {
- FILE *in = fopen(argv[1], "r");
- char buffer[50];
- float payprob[2][20], sum = 0, result;
- int trials = atoi(argv[2]), hands = atoi(argv[3]),
- worst = atoi(argv[4]), i = 0, j, k, total,
- *run = (int *)malloc(hands*sizeof(int)),
- *low = (int *)malloc(trials*sizeof(int)),
- *final = (int *)malloc(trials*sizeof(int));
- srand(time(NULL));
- while (fgets(buffer,sizeof(buffer),in))
- {
- sscanf(buffer, "%f %f", &payprob[0][i], &payprob[1][i]);
- i++;
- }
- total = i;
- for(i = 0;i < total; i++)
- sum += payprob[1][i];
- payprob[1][0] = sum - payprob[1][0];
- for(i = 1; i < total; i++)
- payprob[1][i] = payprob[1][i-1] - payprob[1][i];
- for(i = 0; i < trials; i++)
- {
- for (j = 0; j < hands; j++)
- {
- result = (double)rand() / (double)RAND_MAX;
- for (k = 0; k < total; k++)
- if (result > payprob[1][k])
- {
- run[j] = (int)payprob[0][k] - 1;
- break;
- }
- }
- final[i] = 0;
- low[i] =0;
- for(j = 0; j < hands; j++)
- {
- final[i] += run[j];
- if (final[i] < low[i])
- low[i] = final[i];
- }
- }
- qsort(low,trials,sizeof(int),comp);
- qsort(final,trials,sizeof(int),comp);
- j = 1;
- k = 1;
- for(i = 0; i < trials ; i++)
- if (j && low[i] > worst)
- {
- printf("%d trials out of %d worse than a loss of %d (%f%%)\n",
- i-1, trials, worst, (float)(i-1)*(float)100/(float)trials);
- j--;
- }
- else if (k && final[i] > worst)
- {
- printf("%d trials out of %d worse than a final result of of %d (%f%%)\n",
- i-1, trials, worst, (float)(i-1)*(float)100/(float)trials);
- k--;
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement