diff --git a/src/probabilistic/Hasher.cc b/src/probabilistic/Hasher.cc index 835cb7e5d7..cfe5f3244b 100644 --- a/src/probabilistic/Hasher.cc +++ b/src/probabilistic/Hasher.cc @@ -119,7 +119,7 @@ DefaultHasher::DefaultHasher(size_t k, Hasher::seed_t seed) for ( size_t i = 1; i <= k; ++i ) { seed_t s = Seed(); - s.h[0] += bro_prng(i); + s.h[0] += zeek::prng(i); hash_functions.push_back(UHF(s)); } } @@ -149,7 +149,7 @@ bool DefaultHasher::Equals(const Hasher* other) const } DoubleHasher::DoubleHasher(size_t k, seed_t seed) - : Hasher(k, seed), h1(seed + bro_prng(1)), h2(seed + bro_prng(2)) + : Hasher(k, seed), h1(seed + zeek::prng(1)), h2(seed + zeek::prng(2)) { } diff --git a/src/util.cc b/src/util.cc index 6660446857..ace3638e59 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1066,7 +1066,7 @@ static bool write_random_seeds(const char* write_file, uint32_t seed, } static bool bro_rand_determistic = false; -static unsigned int bro_rand_state = 0; +static long int bro_rand_state = 0; static bool first_seed_saved = false; static unsigned int first_seed = 0; @@ -1183,10 +1183,12 @@ bool have_random_seed() return bro_rand_determistic; } -unsigned int bro_prng(unsigned int state) +long int zeek::prng(long int state) { // Use our own simple linear congruence PRNG to make sure we are // predictable across platforms. (Lehmer RNG, Schrage's method) + // Note: the choice of "long int" storage type for the state is mostly + // for parity with the possible return values of random(). constexpr uint32_t m = 2147483647; constexpr uint32_t a = 16807; constexpr uint32_t q = m / a; @@ -1204,12 +1206,17 @@ unsigned int bro_prng(unsigned int state) return res; } +unsigned int bro_prng(unsigned int state) + { + return zeek::prng(state); + } + long int bro_random() { if ( ! bro_rand_determistic ) return random(); // Use system PRNG. - bro_rand_state = bro_prng(bro_rand_state); + bro_rand_state = zeek::prng(bro_rand_state); return bro_rand_state; } diff --git a/src/util.h b/src/util.h index 3bfee23e6e..b29a80d64b 100644 --- a/src/util.h +++ b/src/util.h @@ -220,6 +220,7 @@ extern bool have_random_seed(); // A simple linear congruence PRNG. It takes its state as argument and // returns a new random value, which can serve as state for subsequent calls. +[[deprecated("Remove in v4.1. Use zeek::prng()")]] unsigned int bro_prng(unsigned int state); // Replacement for the system random(), to which is normally falls back @@ -588,4 +589,14 @@ namespace zeek { */ void set_thread_name(const char* name, pthread_t tid = pthread_self()); +/** + * A platform-independent PRNG implementation. Note that this is not + * necessarily a "statistically sound" implementation as the main purpose is + * not for production use, but rather for regression testing. + * @param state The value used to generate the next random number. + * @return A new random value generated from *state* and that can passed + * back into subsequent calls to generate further random numbers. + */ +long int prng(long int state); + } // namespace zeek