mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Fixing random number generation so that it returns same numbers as
before. That broke a lot of tests.
This commit is contained in:
parent
599dadf30b
commit
d8226169b8
3 changed files with 16 additions and 4 deletions
16
src/H3.h
16
src/H3.h
|
@ -66,18 +66,30 @@
|
||||||
template <typename T, int N>
|
template <typename T, int N>
|
||||||
class H3 {
|
class H3 {
|
||||||
public:
|
public:
|
||||||
H3(T seed = bro_random())
|
H3()
|
||||||
|
{
|
||||||
|
Init(false, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
H3(T seed)
|
||||||
|
{
|
||||||
|
Init(true, seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Init(bool have_seed, T seed)
|
||||||
{
|
{
|
||||||
T bit_lookup[N * CHAR_BIT];
|
T bit_lookup[N * CHAR_BIT];
|
||||||
|
|
||||||
for ( size_t bit = 0; bit < N * CHAR_BIT; bit++ )
|
for ( size_t bit = 0; bit < N * CHAR_BIT; bit++ )
|
||||||
{
|
{
|
||||||
bit_lookup[bit] = 0;
|
bit_lookup[bit] = 0;
|
||||||
seed = bro_prng(seed);
|
|
||||||
for ( size_t i = 0; i < sizeof(T)/2; i++ )
|
for ( size_t i = 0; i < sizeof(T)/2; i++ )
|
||||||
|
{
|
||||||
|
seed = have_seed ? bro_prng(seed) : bro_random();
|
||||||
// assume random() returns at least 16 random bits
|
// assume random() returns at least 16 random bits
|
||||||
bit_lookup[bit] = (bit_lookup[bit] << 16) | (seed & 0xFFFF);
|
bit_lookup[bit] = (bit_lookup[bit] << 16) | (seed & 0xFFFF);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for ( size_t byte = 0; byte < N; byte++ )
|
for ( size_t byte = 0; byte < N; byte++ )
|
||||||
{
|
{
|
||||||
|
|
|
@ -829,7 +829,7 @@ bool have_random_seed()
|
||||||
return bro_rand_determistic;
|
return bro_rand_determistic;
|
||||||
}
|
}
|
||||||
|
|
||||||
long int bro_prng(long int state)
|
unsigned int bro_prng(unsigned 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.
|
// predictable across platforms.
|
||||||
|
|
|
@ -175,7 +175,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.
|
||||||
long int bro_prng(long 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
|
||||||
// except when a seed has been given. In that case, the function bro_prng.
|
// except when a seed has been given. In that case, the function bro_prng.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue