]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add statistical_prng_range() to get a random number over a range
authorWilly Tarreau <w@1wt.eu>
Sat, 17 Apr 2021 13:49:34 +0000 (15:49 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 19 Apr 2021 13:24:33 +0000 (15:24 +0200)
This is simply a multiply and shift from statistical_prng() but it's made
easily accessible.

include/haproxy/tools.h

index 3121fea51a09d444d8bfbbd9e7a4e990754a70fe..e09e17e644e53146022d8aaaccc3efc631383eca 100644 (file)
@@ -1073,6 +1073,14 @@ static inline unsigned int statistical_prng()
        return statistical_prng_state = x;
 }
 
+/* returns a random number between 0 and <range> - 1 that is evenly distributed
+ * over the range.
+ */
+static inline uint statistical_prng_range(uint range)
+{
+       return mul32hi(statistical_prng(), range);
+}
+
 /* Update array <fp> with the character transition <prev> to <curr>. If <prev>
  * is zero, it's assumed that <curr> is the first character. If <curr> is zero
  * its assumed to mark the end. Both may be zero. <fp> is a 1024-entries array