blob: 368718c9f95f7458c120c2f3e8ba0ac0c1aa3518 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#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_int_init(unsigned int seed)
{
boost::lock_guard<boost::mutex> lock(rand_lock);
srand(seed);
}
void rand_int_init()
{
rand_int_init(time(NULL));
}
int rand_int()
{
boost::lock_guard<boost::mutex> lock(rand_lock);
return rand();
}
} // namespace util
|