Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created April 11, 2017 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skeeto/aaf7033c3b84d19ca8ad11efb69aa995 to your computer and use it in GitHub Desktop.
Save skeeto/aaf7033c3b84d19ca8ad11efb69aa995 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#define A 10
#define B 30
#define C 5
#define N 1
#define RULE_B
#define M 1000000L
static uint64_t
xorshift(uint64_t *state)
{
uint64_t x = *state;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
*state = x;
return x * UINT64_C(2685821657736338717);
}
static int
stick_time(uint64_t *s)
{
return xorshift(s) % (B - A + 1) + A + C;
}
static int
simulate(uint64_t *s)
{
int t = 0;
int laps_a = 0;
int laps_b = 0;
int time_a = stick_time(s) - C;
int time_b = stick_time(s) - C;
while (abs(laps_a - laps_b) <= N) {
if (time_a == time_b) {
t += time_a;
time_a = stick_time(s);
time_b = stick_time(s);
laps_a++;
laps_b++;
#ifdef RULE_B
if (abs(laps_a - laps_b) == N)
break;
#endif
} else if (time_a < time_b) {
t += time_a;
time_b -= time_a;
laps_a++;
time_a = stick_time(s);
} else if (time_b < time_a) {
t += time_b;
time_a -= time_b;
laps_b++;
time_b = stick_time(s);
}
}
return t;
}
int
main(void)
{
uint64_t s[1] = {UINT64_C(0xdeadbeefcafebabe)};
double average = 0.0;
*s ^= time(0);
for (long i = 0; i < M; i++)
average += simulate(s) / (double)M;
printf("%f\n", average);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment