Fixing random number generation so that it returns same numbers as

before.

That broke a lot of tests.
This commit is contained in:
Robin Sommer 2013-07-24 16:34:52 -07:00
parent 599dadf30b
commit d8226169b8
3 changed files with 16 additions and 4 deletions

View file

@ -66,17 +66,29 @@
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++ )

View file

@ -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.

View file

@ -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.