From: Amos Jeffries Date: Sun, 8 Feb 2015 13:55:29 +0000 (-0800) Subject: Convert ACL random to C++11 random features X-Git-Tag: merge-candidate-3-v1~109^2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=20b100256526e701b8f7e5929d8e4c08ea67b68d;p=thirdparty%2Fsquid.git Convert ACL random to C++11 random features --- diff --git a/src/acl/Random.cc b/src/acl/Random.cc index 45fd0b2b4d..22b21ed036 100644 --- a/src/acl/Random.cc +++ b/src/acl/Random.cc @@ -15,6 +15,8 @@ #include "Parsing.h" #include "wordlist.h" +#include + ACL * ACLRandom::clone() const { @@ -82,7 +84,7 @@ ACLRandom::parse() } else if (sscanf(t, "%[0-9]/%[0-9]", bufa, bufb) == 2) { int a = xatoi(bufa); int b = xatoi(bufb); - if (a <= 0 || b <= 0) { + if (a <= 0 || b <= 0 || a > b) { debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'"); return; } else @@ -101,8 +103,14 @@ ACLRandom::parse() int ACLRandom::match(ACLChecklist *) { - // make up the random value - double random = ((double)rand() / (double)RAND_MAX); + // make up the random value. + // The fixed-value default seed is fine because we are + // actually matching whether the random value is above + // or below the configured threshold ratio. + static std::mt19937 mt; + static std::uniform_real_distribution<> dist(0, 1); + + const double random = dist(mt); debugs(28, 3, "ACL Random: " << name << " " << pattern << " test: " << data << " > " << random << " = " << ((data > random)?"MATCH":"NO MATCH") ); return (data > random)?1:0;