11 probdist_init(struct probdist
*pd
, int n
)
13 if (!pd
) pd
= malloc(sizeof(*pd
));
15 pd
->items
= calloc(n
, sizeof(pd
->items
[0]));
21 probdist_pick(struct probdist
*pd
)
23 assert(pd
->total
> -1); // -1..0 is rounding error
24 if (pd
->total
< __FLT_EPSILON__
)
26 float stab
= (float) fast_random(65536) / 65536 * pd
->total
;
28 //fprintf(stderr, "stab %f / %f\n", stab, pd->total);
29 for (int i
= 0; i
< pd
->n
; i
++) {
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);
42 probdist_done(struct probdist
*pd
) {