All pastes #2081911 Raw Edit

Something

public text v1 · immutable
#2081911 ·published 2011-09-21 19:25 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>

#define MAX_ROLLS 100000
#define HIGH 6
#define LOW 1

int main(int argc, char **argv) {
  /*
    Declare variable to hold seconds on clock.
  */
  time_t seconds;

  /*
    Get value from system clock and
    place in seconds variable.
  */
  time(&seconds);
  /*
    Convert seconds to a unsigned
    integer.
  */
  srand((unsigned int) seconds);

  printf("Average for 1, 1: %d; Average for 1, 2: %d\n", get_avg(1, 1),
    get_avg(1, 2));

  return 0;
}

int get_avg(int a, int b) {
  int i, total = 0;
  for(i = 0; i < MAX_ROLLS; i++) {
    int n_rolls = do_rolls(a, b);
    total += n_rolls;
  }

  return total / MAX_ROLLS;
}


int do_rolls(int a, int b) {
  int prev = 0, die = 0, n = 0;

  do {
    prev = die;
    die = roll();
    n++;
  } while(prev != a && die != b);

  return n;
}

int roll() {
  return rand() % (HIGH - LOW + 1) + LOW;
}