X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvppinfra%2Frandom.c;h=ea89f0a161898a299195cb38f4368ff6e024769b;hb=e5ecf3ea4b456afb710f4ed903cd7e4c1ae87859;hp=fa5bcc8c78aaa6496a68729d86ab8a1e6229a8f3;hpb=7cd468a3d7dee7d6c92f69a0bb7061ae208ec727;p=vpp.git diff --git a/src/vppinfra/random.c b/src/vppinfra/random.c index fa5bcc8c78a..ea89f0a1618 100644 --- a/src/vppinfra/random.c +++ b/src/vppinfra/random.c @@ -37,11 +37,62 @@ #include -/* Default random seed for standalone version of library. +/** \file Random number support + */ + +/** \brief Default random seed for standalone version of library. Value can be overridden by platform code from e.g. machine's clock count register. */ u32 standalone_random_default_seed = 1; +/** + * \brief Compute the X2 test statistic for a vector of counts. + * Each value element corresponds to a histogram bucket. + * + * Typical use-case: test the hypothesis that a set of octets + * are uniformly distributed (aka random). + * + * In a 1-dimensional use-case, the result should be compared + * with the critical value from chi square tables with + * vec_len(values) - 1 degrees of freedom. + * + * @param[in] values vector of histogram bucket values + * @return d - Pearson's X2 test statistic + */ + +f64 +clib_chisquare (u64 * values) +{ + u32 i, len; + f64 d, delta_d, actual_frequency, expected_frequency; + u64 n_observations = 0; + + len = vec_len (values); + /* + * Shut up coverity. Return a huge number which should always exceed + * the X2 critical value. + */ + if (len == 0) + return (f64) 1e70; + + for (i = 0; i < len; i++) + n_observations += values[i]; + + expected_frequency = (1.0 / (f64) len) * (f64) n_observations; + + d = 0.0; + + for (i = 0; i < len; i++) + { + actual_frequency = ((f64) values[i]); + delta_d = ((actual_frequency - expected_frequency) + * (actual_frequency - expected_frequency)) + / expected_frequency; + d += delta_d; + } + return d; +} + /* * fd.io coding-style-patch-verification: ON *