#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "TRandom3.h"

#define nposs 7
#define nplayer 4

#define ntrial 1000000

#define IDEAL

TRandom3 r(0);

static inline double randdouble()
{
  return r.Rndm();
//  return random()/((double)RAND_MAX+1); // not good enough!
}

static int rand2i(const double r)
{
#ifdef IDEAL
  return (int)(r*nposs);
#endif

  // approximate fractions for my set
  if(r < 0.130) return 0;
  if(r < 0.268) return 1;
  if(r < 0.417) return 2;
  if(r < 0.563) return 3;
  if(r < 0.709) return 4;
  if(r < 0.863) return 5;
  return 6;
}

int main()
{
  const int poss[nposs] = {+1, +2, +3, +4, -2, -2, -10};

  int t;
  printf("winner nplies\n");
  for(t = 0; t < ntrial; t++){
    int cherries[nplayer];
    memset(cherries, 0, sizeof(int)*nplayer);
    int winner = -1, nplies = 0;
   
    for(; ;){
      int i;
      int done = 0;
      for(i = 0; i < nplayer; i++){
        cherries[i] += poss[rand2i(randdouble())];
        if(cherries[i] < 0){
          cherries[i] = 0;
        }
        else if(cherries[i] >= 10){
          if(!done) winner = i;
          done = 1;
          nplies++;
          break;
        }
        if(!done) nplies++;
      }
      if(done) break;
    } 
    printf("%d %d\n", winner+1, nplies);
  }
  return 0;
}
