Ensured the seed of the simple RNG is at most 31-bit wide.

This commit is contained in:
Guillaume Melquiond 2010-12-21 10:13:12 +00:00
parent f661a5161c
commit 7296c4b8a3
2 changed files with 16 additions and 16 deletions

View File

@ -237,20 +237,16 @@ set_random_generator::~set_random_generator()
}
simple_rng::simple_rng() :
random_seed_(rand()),
random_pool_(random_seed_),
random_calls_(0)
random_seed_(rand() & 0x7FFFFFFF),
random_pool_(random_seed_),
random_calls_(0)
{
}
simple_rng::simple_rng(const config& cfg) :
/**
* @todo older savegames don't have random_seed stored, evaluate later
* whether default can be removed again. Look after branching 1.5.
*/
random_seed_(cfg["random_seed"].to_int(42)),
random_pool_(random_seed_),
random_calls_(0)
random_seed_(cfg["random_seed"]),
random_pool_(random_seed_),
random_calls_(0)
{
}
@ -262,7 +258,7 @@ int simple_rng::get_next_random()
<< " for call " << random_calls_
<< " with seed " << random_seed_ << '\n';
return (static_cast<unsigned>(random_pool_ / 65536) % 32768);
return (random_pool_ / 65536) % 32768;
}
void simple_rng::seed_random(const unsigned call_count)
@ -270,6 +266,12 @@ void simple_rng::seed_random(const unsigned call_count)
seed_random(random_seed_, call_count);
}
void simple_rng::rotate_random()
{
random_seed_ = random_pool_ & 0x7FFFFFFF;
random_calls_ = 0;
}
void simple_rng::seed_random(const int seed, const unsigned call_count)
{
random_pool_ = seed;

View File

@ -57,9 +57,7 @@ public:
* this way we stay in the same sequence but don't have a lot
* calls. Used when moving to the next scenario.
*/
void rotate_random()
{ random_seed_ = random_pool_; random_calls_ = 0; }
void rotate_random();
int get_random_seed() const { return random_seed_; }
int get_random_calls() const { return random_calls_; }
@ -68,8 +66,8 @@ private:
/** Initial seed for the pool. */
int random_seed_;
/** State for the random pool. */
int random_pool_;
/** State for the random pool (modulo arithmetic). */
unsigned random_pool_;
/** Number of time a random number is generated. */
unsigned random_calls_;