+12
-7
lines changedFilter options
+12
-7
lines changed Original file line number Diff line number Diff line change
@@ -283,10 +283,11 @@ bool srt::sync::CGlobEvent::waitForEvent()
283
283
namespace srt
284
284
{
285
285
#if HAVE_CXX11
286
-
static std::random_device& randomDevice()
286
+
static std::mt19937& randomGen()
287
287
{
288
288
static std::random_device s_RandomDevice;
289
-
return s_RandomDevice;
289
+
static std::mt19937 s_GenMT19937(s_RandomDevice());
290
+
return s_GenMT19937;
290
291
}
291
292
#elif defined(_WIN32) && defined(__MINGW32__)
292
293
static void initRandSeed()
@@ -300,7 +301,7 @@ static pthread_once_t s_InitRandSeedOnce = PTHREAD_ONCE_INIT;
300
301
static unsigned int genRandSeed()
301
302
{
302
303
// Duration::count() does not depend on any global objects,
303
-
// therefore it is preferred over count)microseconds(..).
304
+
// therefore it is preferred over count_microseconds(..).
304
305
const int64_t seed = sync::steady_clock::now().time_since_epoch().count();
305
306
return (unsigned int) seed;
306
307
}
@@ -325,7 +326,7 @@ int srt::sync::genRandomInt(int minVal, int maxVal)
325
326
sync::ScopedLock lck(s_mtxRandomDevice);
326
327
#if HAVE_CXX11
327
328
uniform_int_distribution<> dis(minVal, maxVal);
328
-
return dis(randomDevice());
329
+
return dis(randomGen());
329
330
#else
330
331
#if defined(__MINGW32__)
331
332
// No rand_r(..) for MinGW.
@@ -342,9 +343,13 @@ int srt::sync::genRandomInt(int minVal, int maxVal)
342
343
#endif
343
344
344
345
// Map onto [minVal, maxVal].
345
-
// Note. The probablity to get maxVal as the result is minuscule.
346
-
const int res = minVal + static_cast<int>((maxVal - minVal) * rand_0_1);
347
-
return res;
346
+
// Note. There is a minuscule probablity to get maxVal+1 as the result.
347
+
// So we have to use long long to handle cases when maxVal = INT32_MAX.
348
+
// Also we must check 'res' does not exceed maxVal,
349
+
// which may happen if rand_0_1 = 1, even though the chances are low.
350
+
const long long llMaxVal = maxVal;
351
+
const int res = minVal + static_cast<int>((llMaxVal + 1 - minVal) * rand_0_1);
352
+
return min(res, maxVal);
348
353
#endif // HAVE_CXX11
349
354
}
350
355
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4