Debian Codegraph @

netem/normal.c

raw
1 /* SPDX-License-Identifier: NIST-PD */
2 /*
3 * Normal distribution table generator
4 * Taken from the uncopyrighted NISTnet code.
5 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <math.h>
9 #include <string.h>
10 #include <limits.h>
11
12 #include <linux/types.h>
13 #include <linux/pkt_sched.h>
14
15 #define TABLESIZE 16384
16 #define TABLEFACTOR NETEM_DIST_SCALE
17
18 static double
19 normal(double x, double mu, double sigma)
20 {
21 return .5 + .5*erf((x-mu)/(sqrt(2.0)*sigma));
22 }
23
24
25 int
26 main(int argc, char **argv)
27 {
28 int i, n;
29 double x;
30 double table[TABLESIZE+1];
31
32 for (x = -10.0; x < 10.05; x += .00005) {
33 i = rint(TABLESIZE * normal(x, 0.0, 1.0));
34 table[i] = x;
35 }
36
37
38 printf("# This is the distribution table for the normal distribution.\n");
39 for (i = n = 0; i < TABLESIZE; i += 4) {
40 int value = (int) rint(table[i]*TABLEFACTOR);
41 if (value < SHRT_MIN) value = SHRT_MIN;
42 if (value > SHRT_MAX) value = SHRT_MAX;
43
44 printf(" %d", value);
45 if (++n == 8) {
46 putchar('\n');
47 n = 0;
48 }
49 }
50
51 return 0;
52 }