Last active
June 10, 2019 11:57
-
-
Save kupp1/760be764e7915d75bf222556e89cb487 to your computer and use it in GitHub Desktop.
Monty Hall problem with gnuplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
P=monty | |
OBJECTS= | |
CFLAGS = -g -Iinclude -Wall `pkg-config --cflags libsodium` | |
LDLIBS= `pkg-config --libs libsodium` | |
CC=cc | |
$(P): $(OBJECTS) | |
clean: | |
-rm $(OBJECTS) $(P) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sodium.h> | |
#include <unistd.h> | |
int main(int argc, char **argv) | |
{ | |
if (sodium_init() < 0) | |
{ | |
printf("Cannot initialize sodium"); | |
return 1; | |
} | |
int opt, doors = 3, attempts = 10; | |
while((opt = getopt(argc, argv, "d:a:")) != -1) | |
{ | |
switch(opt) | |
{ | |
case 'd': doors = atoi(optarg); break; | |
case 'a': attempts = atoi(optarg); break; | |
} | |
} | |
printf("USAGE :\n\t%s -d [int doors > 3] " | |
"-a [int attempts > 1] | gnuplot --persist\n", argv[0]); | |
if (doors < 3 || attempts < 1) return 1; | |
int first_choice_wins = 0, second_choice_wins = 0; | |
FILE *fcw = fopen("fcw.csv", "w+"), *scw = fopen("scw.csv", "w+"); | |
for (int i = 0; i < attempts; i++) | |
{ | |
int car = randombytes_uniform(doors); | |
int player_first_choice = randombytes_uniform(doors); | |
if (player_first_choice == car) | |
first_choice_wins++; | |
second_choice_wins++; | |
fprintf(fcw, "%d, %d\n", i + 1, first_choice_wins); | |
fprintf(scw, "%d, %d\n", i + 1, second_choice_wins); | |
} | |
fclose(fcw); | |
fclose(scw); | |
printf("set colorsequence classic\n"); | |
printf("set title 'Monty Hall paradox with %d doors and %d attempts'\n", | |
doors, attempts); | |
puts("set xlabel 'attempts'\nset ylabel 'total wins count'"); | |
printf("set xrange[0:%d]\n", attempts + 1); | |
printf("set yrange[0:%d]\n", attempts + 1); | |
puts("plot 'fcw.csv' with lines lw 5 lc rgb 'blue', " | |
"'scw.csv' with lines lw 5 lc rgb 'red'\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment