]> thyrsus.com Git - deal.git/commitdiff
Initial revision
authorEric S. Raymond <esr@thyrsus.com>
Thu, 13 Apr 1995 04:23:11 +0000 (04:23 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 13 Apr 1995 04:23:11 +0000 (04:23 +0000)
deal.c [new file with mode: 0644]

diff --git a/deal.c b/deal.c
new file mode 100644 (file)
index 0000000..1cad229
--- /dev/null
+++ b/deal.c
@@ -0,0 +1,65 @@
+#include <math.h>
+
+main(argc, argv)
+    int argc;
+    char **argv;
+{
+    int turns, special, total, columns, i, j;
+    double hyper_prob();
+
+    if  (argc == 1) {
+        printf("Help for Cardprobs : here's the order of the arguments:\n");
+        printf("     # of turns; # special cards; # total cards;\n");
+        printf("     # columns.\n");
+        printf("No arguments are optional.\n");
+    } else {
+        turns = atoi(argv[1]);
+        special = atoi(argv[2]);
+        total = atoi(argv[3]);
+        if (total < special) {
+            printf("can't have more specials than total cards!\n");
+            exit(-1);
+        }
+        columns = atoi(argv[4]);
+        if (columns > special-1) {
+            printf("using %d columns because can only have\n",special+1);
+            printf("0 to %d specials\n",special);
+            columns = special+1;
+        }
+                
+        printf("%d total cards; %d special cards\n\n\n",total,special);
+
+        printf("       |  Prob of this number of specials\n");
+        printf("Turn # | ");
+        for (j = 0; j < columns; j++)
+            printf("  %2d   ",j);
+        printf("\n");
+
+        for (i = 1; i < turns+1; i++) {
+            printf("%2d     | ",i);
+            for (j = 0; (j < columns) && (j < i+8) && (j < special+1); j++)
+                printf("%5.4f ",hyper_prob(j,i+7,special,total));
+            for (j= i+8; j < columns; j++)
+                printf("   *   ");
+            printf("\n");
+        }
+    }
+}
+
+double hyper_prob(x, k, M, N)
+    int x, k, M, N;
+{
+    double exp(), log_a_choose_b();
+
+    return(exp(log_a_choose_b(M,x) + log_a_choose_b(N-M,k-x) -
+                        log_a_choose_b(N, k)));
+}
+
+double log_a_choose_b(a,b)
+    int a, b;
+{
+    double lgamma();
+
+    return(lgamma((double) (a+1)) - lgamma((double) (b+1)) -
+                lgamma((double) (a-b+1)));
+}