11 probdist_init(struct probdist
*pd
, int n
)
13 if (!pd
) pd
= malloc(sizeof(*pd
));
15 pd
->items
= calloc(n
, sizeof(pd
->items
[0]));
16 pd
->items
[0] = 0; // probdist_set() requires [0] to be initialized
21 probdist_pick(struct probdist
*pd
)
23 assert(pd
->items
[pd
->n
- 1] >= 0);
24 /* TODO: float random */
25 float stab
= (float) fast_random(65536) / 65536 * pd
->items
[pd
->n
- 1];
26 //fprintf(stderr, "stab %f / %f\n", stab, pd->items[pd->n - 1]);
27 for (int i
= 0; i
< pd
->n
; i
++) {
28 if (stab
<= pd
->items
[i
])
31 //fprintf(stderr, "overstab %f (total %f, sum %f)\n", stab, pd->items[pd->n - 1], sum);
37 probdist_done(struct probdist
*pd
) {