Deprecate bro_prng(), replace with zeek::prng()

The type used for storing the state of the RNG is changed from
`unsigned int` to `long int` since the former has a minimal range
of [0, 65,535] while the RNG function itself has a range of
[1, 2147483646].  A `long int` must be capable of
[−2147483647, +2147483647] and is also the return type of `random()`,
which is what zeek::prng() aims to roughly parity.
This commit is contained in:
Jon Siwek 2020-07-22 09:13:34 -07:00
parent 887b53b7f3
commit 6bbb0a6b48
3 changed files with 23 additions and 5 deletions

View file

@ -119,7 +119,7 @@ DefaultHasher::DefaultHasher(size_t k, Hasher::seed_t seed)
for ( size_t i = 1; i <= k; ++i ) for ( size_t i = 1; i <= k; ++i )
{ {
seed_t s = Seed(); seed_t s = Seed();
s.h[0] += bro_prng(i); s.h[0] += zeek::prng(i);
hash_functions.push_back(UHF(s)); 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) 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))
{ {
} }

View file

@ -1066,7 +1066,7 @@ static bool write_random_seeds(const char* write_file, uint32_t seed,
} }
static bool bro_rand_determistic = false; 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 bool first_seed_saved = false;
static unsigned int first_seed = 0; static unsigned int first_seed = 0;
@ -1183,10 +1183,12 @@ bool have_random_seed()
return bro_rand_determistic; 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 // Use our own simple linear congruence PRNG to make sure we are
// predictable across platforms. (Lehmer RNG, Schrage's method) // 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 m = 2147483647;
constexpr uint32_t a = 16807; constexpr uint32_t a = 16807;
constexpr uint32_t q = m / a; constexpr uint32_t q = m / a;
@ -1204,12 +1206,17 @@ unsigned int bro_prng(unsigned int state)
return res; return res;
} }
unsigned int bro_prng(unsigned int state)
{
return zeek::prng(state);
}
long int bro_random() long int bro_random()
{ {
if ( ! bro_rand_determistic ) if ( ! bro_rand_determistic )
return random(); // Use system PRNG. 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; return bro_rand_state;
} }

View file

@ -220,6 +220,7 @@ extern bool have_random_seed();
// A simple linear congruence PRNG. It takes its state as argument and // 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. // 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); unsigned int bro_prng(unsigned int state);
// Replacement for the system random(), to which is normally falls back // 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()); 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 } // namespace zeek