Pattern: New default set, including gammaf, generated from sample of tygem high-dan...
[pachi.git] / probdist.c
blobd5dad2fdc68b00ef41336940957381ce196139ff
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
6 #include "move.h"
7 #include "probdist.h"
8 #include "random.h"
10 struct probdist *
11 probdist_init(struct probdist *pd, int bsize2)
13 if (!pd) pd = malloc(sizeof(*pd));
14 pd->bsize2 = bsize2;
15 pd->moves = calloc(bsize2, sizeof(pd->moves[0]));
16 pd->total = 0;
17 return pd;
20 coord_t
21 probdist_pick(struct probdist *pd)
23 assert(pd->total > -1); // -1..0 is rounding error
24 if (pd->total < __FLT_EPSILON__)
25 return pass;
26 float stab = (float) fast_random(65536) / 65536 * pd->total;
27 float sum = 0;
28 //fprintf(stderr, "stab %f / %f\n", stab, pd->total);
29 for (coord_t c = 0; c < pd->bsize2; c++) {
30 sum += pd->moves[c];
31 if (stab < sum)
32 return c;
34 //fprintf(stderr, "overstab %f (total %f, sum %f)\n", stab, pd->total, sum);
35 // This can sometimes happen when also punching due to rounding errors,.
36 // Just assert that the difference is tiny.
37 assert(fabs(pd->total - stab) < 1);
38 return pass;
41 void
42 probdist_done(struct probdist *pd) {
43 free(pd->moves);