#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define BASE_ID 745711
char* names[50] = { "Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
"Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada",
"New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
"West Virginia", "Wisconsin", "Wyoming" };
int votes[50] = { 9, 3, 11, 6, 55, 9, 7, 3, 29, 16, 4, 4, 20, 11, 6, 6, 8, 8, 4,
10, 11, 16, 10, 6, 10, 3, 5, 6, 4, 14, 5, 29, 15, 3, 18, 7, 7, 20, 4, 9,
3, 11, 38, 6, 3, 13, 12, 5, 10, 3
};
double get_pct(int id) {
char get_str[128];
char buffer[1024];
char number[12];
int j;
FILE* fp;
char* ptr;
double pct;
id += BASE_ID;
sprintf(get_str,
"wget
Intrade - Markets index.html?contractId=%i tmp.dat",
id, id);
system(get_str);
fp = fopen("tmp.dat", "rt");
for (j = 0; j < 305; j++) {
fgets(buffer, 1024, fp);
}
ptr = number;
for (j = 0; j <= strlen(buffer); j++) {
if (buffer[j] == '.' || isdigit(buffer[j])) {
*ptr = buffer[j];
ptr++;
}
}
*ptr = '\0';
//printf("%s\n", number);
pct = atof(number);
// printf("%f\n", pct);
fclose(fp);
system("rm tmp.dat");
}
double state_dem_odds(int i) {
double pct_r, pct_d, tot;
pct_d = get_pct(3 * i);
pct_r = get_pct(3 * i + 1);
tot = 0.0;
if (pct_r != 0.0) {
tot += pct_r;
}
if (pct_d != 0.0) {
tot += pct_d;
}
pct_d /= tot;
return pct_d;
}
double rand1() {
return ((double) (rand() & 0xFFFFFF) / (double) 0xFFFFFF);
}
int trial_dem_win(double odds[50], int* vcnt) {
int i;
int dcnt;
int rcnt;
dcnt = 3;
rcnt = 0;
for (i = 0; i < 50; i++) {
if (rand1() < odds
) {
dcnt += votes;
} else {
rcnt += votes;
}
}
// printf("%i %i\n", dcnt, rcnt);
*vcnt = dcnt;
if (dcnt > rcnt) {
return 1;
} else if (dcnt < rcnt) {
return -1;
} else {
return 0;
}
}
int main(void) {
double D_odds[50];
int i;
int dw, rw, nw, dcnt;
double dcnt_avg;
int N = 10000000;
int w;
srand(time(NULL));
for (i = 0; i < 50; i++) {
D_odds = state_dem_odds(i);
}
for (i = 0; i < 50; i++) {
printf("%20s %i %f\n", names, votes, 100.0 * D_odds);
}
dcnt_avg= 0.0;
dw = rw = nw = 0;
for (i = 1; i <= N; i++) {
w = trial_dem_win(D_odds, &dcnt);
dcnt_avg = (dcnt_avg * (double) (i - 1) + (double) dcnt) / (double) i;
if (w > 0) {
dw++;
} else if (w < 0) {
rw++;
} else {
nw++;
}
if (i % 1000 == 0) {
printf("D: %i/%f R: %i/%f T: %i/%f %f \n", dw, dw / (double) i, rw,
rw / (double) i, nw, nw / (double) i, dcnt_avg);
}
}
}