Skip to content

Instantly share code, notes, and snippets.

@jsharf
Created October 24, 2013 04:13
Show Gist options
  • Save jsharf/7131266 to your computer and use it in GitHub Desktop.
Save jsharf/7131266 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand(time(0));
// successes is the number of times the "smart"
// method correctly guesses >/<. dumb_successes is the
// number of times the trivial 50-50 method is correct
int i, successes = 0, dumb_successes = 0, total_trials=100000;
for (i=0; i<total_trials; i++)
{
// generate first and second random numbers
int first = rand(), second = rand();
// generate third random number
int x = rand();
// if third random number > first random number
if (x > first)
{
// then our guess is that the second number is also greater
if (second > first)
{
successes++;
}
}
// if third random number < first random number
if (x < first)
{
// then our guess is that hte second number is also less
if (second < first)
{
successes++;
}
}
// now for the dumb method: arbitrarily chose greater than or less than
// To chose this, I just use x%2. odd means guess "greater", even means
// guess "less than"
// greater than
if (x%2 == 1)
{
// if correct, increment dumb successes
if (second > first)
{
dumb_successes++;
}
}
// less than
if (x%2 == 0)
{
if (second < first)
{
dumb_successes++;
}
}
}
printf("Smart method: %f\n", ((double) successes)/total_trials);
printf("Dumb method: %f\n", ((double) dumb_successes)/total_trials);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment