diff --git a/MonteCarlo.c b/MonteCarlo.c new file mode 100644 index 0000000..2ec2823 --- /dev/null +++ b/MonteCarlo.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +struct Simulation { + double betsize; + double STARTING_BANKROLL; + double bankroll; + int attempts; +}; + +int roll() { + return (random() % 7) + 1; +} + +double handle(int player, int house) { + int diff = house - player; + if (diff >= 1) {return 0;} + else if (diff == -6) {return 2;} + else if (diff == 0) {return 0;} + else {return 1.5;} +} + +bool calculate(struct Simulation* simulation) { + if (simulation->bankroll >= simulation->betsize) { + simulation->bankroll -= simulation->betsize; + int player_roll = roll(); + int house_roll = roll(); + double modifier = handle(player_roll, house_roll); + simulation->bankroll += (modifier * simulation->betsize); + simulation->attempts++; + return false; + } else {return true;} +} + +void run(struct Simulation* simulation, int attempts) { + for (int i = 0; i < attempts; i++) { + bool bankrupt = calculate(simulation); + if (bankrupt) { + break; + } + } +} + +int main() { + srandom(time(NULL)); + struct Simulation new_simulation; + new_simulation.betsize = 20; + new_simulation.STARTING_BANKROLL = 5000000000; + new_simulation.bankroll = new_simulation.STARTING_BANKROLL; + new_simulation.attempts = 0; + run(&new_simulation, 2147483647); + printf("Starting Bankroll: %f\n", new_simulation.STARTING_BANKROLL); + printf("Current Bankroll: %f\n", new_simulation.bankroll); + printf("Finished after %d attempts and with a betsize of: %f\n", new_simulation.attempts, new_simulation.betsize); + return 0; +}