summaryrefslogtreecommitdiff
path: root/indra/llcommon/llrand.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'indra/llcommon/llrand.cpp')
-rw-r--r--indra/llcommon/llrand.cpp14
1 files changed, 11 insertions, 3 deletions
diff --git a/indra/llcommon/llrand.cpp b/indra/llcommon/llrand.cpp
index 33afc50cf7..0192111574 100644
--- a/indra/llcommon/llrand.cpp
+++ b/indra/llcommon/llrand.cpp
@@ -58,7 +58,9 @@
* to restore uniform distribution.
*/
-static LLRandLagFib2281 gRandomGenerator(LLUUID::getRandomSeed());
+// gRandomGenerator is a stateful static object, which is therefore not
+// inherently thread-safe.
+static thread_local LLRandLagFib2281 gRandomGenerator(LLUUID::getRandomSeed());
// no default implementation, only specific F64 and F32 specializations
template <typename REAL>
@@ -71,7 +73,7 @@ inline F64 ll_internal_random<F64>()
// CPUs (or at least multi-threaded processes) seem to
// occasionally give an obviously incorrect random number -- like
// 5^15 or something. Sooooo, clamp it as described above.
- F64 rv = gRandomGenerator();
+ F64 rv{ gRandomGenerator() };
if(!((rv >= 0.0) && (rv < 1.0))) return fmod(rv, 1.0);
return rv;
}
@@ -79,7 +81,13 @@ inline F64 ll_internal_random<F64>()
template <>
inline F32 ll_internal_random<F32>()
{
- return F32(ll_internal_random<F64>());
+ // *HACK: clamp the result as described above.
+ // Per Monty, it's important to clamp using the correct fmodf() rather
+ // than expanding to F64 for fmod() and then truncating back to F32. Prior
+ // to this change, we were getting sporadic ll_frand() == 1.0 results.
+ F32 rv{ narrow<F32>(gRandomGenerator()) };
+ if(!((rv >= 0.0f) && (rv < 1.0f))) return fmodf(rv, 1.0f);
+ return rv;
}
/*------------------------------ F64 aliases -------------------------------*/