mosesdecoder/util/random.cc
Jeroen Vermeulen 38d790cac0 Add cross-platform randomizer module.
The code uses two mechanisms for generating random numbers: srand()/rand(),
which is not thread-safe, and srandom()/random(), which is POSIX-specific.

Here I add a util/random.cc module that centralizes these calls, and unifies
some common usage patterns.  If the implementation is not good enough, we can
now change it in a single place.

To keep things simple, this uses the portable srand()/rand() but protects them
with a lock to avoid concurrency problems.

The hard part was to keep the regression tests passing: they rely on fixed
sequences of random numbers, so a small code change could break them very
thoroughly.  Util::rand(), for wide types like size_t, calls std::rand() not
once but twice.  This behaviour was generalized into utils::wide_rand() and
friends.
2015-04-23 23:46:04 +07:00

44 lines
871 B
C++

#include "util/random.hh"
#include <cstdlib>
#include <boost/thread/locks.hpp>
#include <boost/thread/lock_guard.hpp>
#include <boost/thread/mutex.hpp>
namespace util
{
namespace
{
/** Lock to protect randomizer.
*
* This module is implemented in terms of rand()/srand() from <cstdlib>.
* These functions are standard C, but they're not thread-safe. Scalability
* is not worth much complexity here, so just slap a mutex around it.
*/
boost::mutex rand_lock;
} // namespace
void rand_init(unsigned int seed)
{
boost::lock_guard<boost::mutex> lock(rand_lock);
srand(seed);
}
void rand_init()
{
rand_init(time(NULL));
}
namespace internal
{
// This is the one call to the actual randomizer. All else is built on this.
int rand_int()
{
boost::lock_guard<boost::mutex> lock(rand_lock);
return std::rand();
}
} // namespace internal
} // namespace util