Needed this for my D&D games

This commit is contained in:
Emma Nora Theuer 2025-02-07 00:11:37 +01:00
parent 47c2d6a63b
commit fdd5db23bf

58
MonteCarlo.c Normal file
View file

@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
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;
}