diff --git a/src/internal_modules/roc_core/fast_random.cpp b/src/internal_modules/roc_core/fast_random.cpp index df99d8350..e827f6b70 100644 --- a/src/internal_modules/roc_core/fast_random.cpp +++ b/src/internal_modules/roc_core/fast_random.cpp @@ -66,5 +66,20 @@ uint32_t fast_random_range(uint32_t from, uint32_t to) { return ret; } +// Gaussian PRNG implementation is based on Box-Muller transform: +// https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform +double fast_random_gaussian() { + // Generate two uniform random numbers + double u1 = (double)fast_random() / (double)UINT32_MAX; + double u2 = (double)fast_random() / (double)UINT32_MAX; + + // Use Box-Muller transform to convert uniform random numbers to normal random numbers + double r = std::sqrt(-2.0 * std::log(u1)); + double theta = 2.0 * M_PI * u2; + + // Return one of the normal random numbers + return r * std::cos(theta); +} + } // namespace core } // namespace roc diff --git a/src/internal_modules/roc_core/fast_random.h b/src/internal_modules/roc_core/fast_random.h index 54f3c6166..14f46b8ef 100644 --- a/src/internal_modules/roc_core/fast_random.h +++ b/src/internal_modules/roc_core/fast_random.h @@ -27,6 +27,11 @@ uint32_t fast_random(); //! @returns random value in inclusive range [from; to]. uint32_t fast_random_range(uint32_t from, uint32_t to); +//! Get a random double from a non cryptographically secure, but fast PRNG. +//! Thread-safe. +//! @returns normally distibure random value with 1 variance. +double fast_random_gaussian(); + } // namespace core } // namespace roc