13 #include <unordered_map>
86 static constexpr uint64_t
no_seed = 0xBAD0000000000000L;
100 const sqlite3_context* db,
104 sqlite3_context* db_ =
const_cast<sqlite3_context*
> (db);
117 std::lock_guard<std::mutex>(h.m_mutex);
120 auto gen_it = h.m_generators.find(db);
123 if (gen_it != h.m_generators.end())
126 return gen_it->second.get();
129 gen_it->second->seed(
static_cast<uint32_t
>(0xFFFFFFFFL & seed));
130 return gen_it->second.get();
137 #ifndef ALLOW_RANDOM_DEVICE_FOR_SEEDING
138 std::cerr <<
"Trying to use unseeded generator. "
139 <<
"Compile with -DALLOW_RANDOM_DEVICE_FOR_SEEDING to ignore."
141 throw std::logic_error(
"Random seeding disabled.");
143 std::random_device seeder;
144 h.m_generators[db] = std::unique_ptr<PRNG>(
new PRNG(seeder()));
145 return h.m_generators[db].get();
149 h.m_generators[db] = std::unique_ptr<PRNG>(
new PRNG(
150 static_cast<uint32_t
>(0xFFFFFFFFL & seed)
153 return h.m_generators[db].get();
160 std::lock_guard<std::mutex>(h.m_mutex);
162 auto it = h.m_generators.find(db);
163 bool releasing_unexisting = (it == h.m_generators.end());
164 h.m_generators.erase(db);
165 return releasing_unexisting;
172 std::unordered_map<const sqlite3*, std::unique_ptr<PRNG> > m_generators;
Singleton handler of Pseudo-Random Number Generator(s)
static PRNG * get_or_create(const sqlite3_context *db, uint64_t seed=no_seed)
Return a pointer to an initialized generator.
static T_GlobalPRNG & handle()
Return a handle to the hash table, not to the single generator (see below).
static bool release(const sqlite3 *db)
Releases a generator (delete). May require re-seeding.
static PRNG * get_or_create(const sqlite3 *db, uint64_t seed=no_seed)
Return a pointer to an initialized generator.
static constexpr uint64_t no_seed
Higher part of the int64 is used to identify missing or invalid seed.
void operator=(T_GlobalPRNG const &)=delete
Copy operator disabled as per singleton pattern.
T_GlobalPRNG(T_GlobalPRNG const &)=delete
Copy constructor disabled as per singleton pattern.
T_GlobalPRNG< std::ranlux48 > GlobalPRNG
Default specialization using RANLUX48.
T_GlobalPRNG< std::mt19937 > GlobalPRNG_MT
An additional specialization for testing purpose using Marsenne Twister.