From 556fea05affe6fe8866557d42b6c1ef6559d7880 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sat, 7 Nov 2026 06:20:46 +0300 Subject: publish --- src/distribution.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/distribution.c (limited to 'src/distribution.c') diff --git a/src/distribution.c b/src/distribution.c new file mode 100644 index 0000000..7887ba3 --- /dev/null +++ b/src/distribution.c @@ -0,0 +1,57 @@ +#define _GNU_SOURCE +#include +#include +#include + +#include "distribution.h" + +void +distribution_init (DISTRIBUTION dist, + unsigned int n, + unsigned int b, + unsigned int r, + int c) +{ + assert (n > 0); + assert (b > 0); + assert (r > 0); + assert (b + r + c*(n - 1) > 0); + + dist->n = n; + dist->b = b; + dist->r = r; + dist->c = c; + dist->p0 = r/(float)(b + r); + for (int i = 1; i < n; ++i) { + dist->p0 *= (r + i*c)/(float)(b + r + i*c); + } +} + +int +distribution_rand (DISTRIBUTION dist) +{ + for (;;) { + double p = dist->p0; + double r = rand ()/(double) RAND_MAX; + // printf ("r = %f, p = %f\n", r, p); + unsigned int x = 0; + + while (x < dist->n - 1) { + r -= p; + if (r < 0) + return x; + x += 1; + p *= ((dist->n - 1 - x) * + (dist->b + (x - 1)*dist->c) /(double) + (x*(dist->r + (dist->n - x)*dist->c))); + // printf ("p = %f\n", p); + } // putchar ('\n'); + } + // return dist->n; +} + +double +distribution_nrand (DISTRIBUTION dist) +{ + return distribution_rand (dist)/(double) (dist->n - 2); +} -- cgit v1.3.1