Skip to content

Instantly share code, notes, and snippets.

@ariedro
Created April 11, 2019 02:02
Show Gist options
  • Save ariedro/9ebace848853340e488a8d3cb1691d9a to your computer and use it in GitHub Desktop.
Save ariedro/9ebace848853340e488a8d3cb1691d9a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define LEN 10
bool play() {
int numbers[LEN];
bool chosens[LEN];
int max_index = 0;
for (int i = 0; i < LEN; i++) {
numbers[i] = rand();
if (numbers[i] > numbers[max_index])
max_index = i;
chosens[i] = false;
}
int tries = rand() % LEN + 1;
int last_option;
for (int i = 0; i < tries; i++) {
last_option = rand() % LEN;
chosens[last_option] = true;
}
return (last_option == max_index);
}
int main() {
FILE *fp = fopen("/dev/urandom", "r");
int true_random;
fread(&true_random, sizeof(int), 1, fp);
srand(true_random);
for (int j = 0; j < 100; j++) {
int wins = 0;
int plays = 0;
for (int i = 0; i < 1000; i++) {
if (play())
wins++;
plays++;
}
printf("%f\n", ((double)wins / (double)plays) * 100);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment