Skip to content

Instantly share code, notes, and snippets.

@Brandon-Rozek
Created May 14, 2021 23:14
Show Gist options
  • Save Brandon-Rozek/de137a70ec6d03dd6da7fd73a7fa6a4c to your computer and use it in GitHub Desktop.
Save Brandon-Rozek/de137a70ec6d03dd6da7fd73a7fa6a4c to your computer and use it in GitHub Desktop.
Monte Cristo Simulation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Amount of times for simulation to run
#define N 5000000
//The amount of times per simulation
#define R 1
//The amount of choices
#define C 4
unsigned short simulate(unsigned int r, unsigned int c);
int main(int argc, char *argv[]) {
//Seed the random machine
srand(time(NULL));
//Count is the number of successful attempts
unsigned int count = 0;
unsigned int total = 0;
for (int i = 0; i < N; i++) {
if (simulate(R,C)) {
count++;
}
total++;
}
printf("Chance: %.2f%%\n", (float)count/(float)total * 100.0);
return 0;
}
unsigned short simulate(unsigned int r, unsigned int c) {
for (unsigned short i = 0; i < r; i++) {
//Return true if random number picks wanted choice
if ((rand() % c) + 1 == c) {
return 1;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment