Skip to content

Commit

Permalink
chore: Port fast_random from doubles to floats
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Aug 11, 2024
1 parent b26f9f0 commit b9d072d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
19 changes: 9 additions & 10 deletions src/internal_modules/roc_core/fast_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ uint64_t fast_random_64() {
return (hi << 32) | lo;
}

// Doubles in [0; 1] have 47-bit precision, so we use 64-bit PRNG
// instead of 32-bit.
double fast_random_float() {
return (double)fast_random_64() / (double)UINT64_MAX;
// Floats in [0; 1] have 24-bit precision, so 32-bit PRNG is enough.
float fast_random_float() {
return (float)fast_random_32() / (float)UINT32_MAX;
}

// Bounded PRNG adaptation of "Bitmask with Rejection (Unbiased) — Apple's Method"
Expand All @@ -77,7 +76,7 @@ uint64_t fast_random_range(uint64_t from, uint64_t to) {
// 0001.......
// 00011......
// 0001111....
// Thanks to @rnovatorov for the hint
// Thanks to @rnovatorov for the hint.
uint64_t mask = range;
mask |= mask >> 1;
mask |= mask >> 2;
Expand All @@ -101,14 +100,14 @@ uint64_t fast_random_range(uint64_t from, uint64_t to) {

// Gaussian PRNG implementation is based on Box-Muller transform:
// https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
double fast_random_gaussian() {
float fast_random_gaussian() {
// Generate two uniform random numbers
const double u1 = fast_random_float();
const double u2 = fast_random_float();
const float u1 = fast_random_float();
const float u2 = fast_random_float();

// Use Box-Muller transform to convert uniform random numbers to normal random numbers
const double r = std::sqrt(-2.0 * std::log(u1));
const double theta = 2.0 * M_PI * u2;
const float r = std::sqrt(-2.0f * std::log(u1));
const float theta = 2.0f * float(M_PI) * u2;

// Return one of the normal random numbers
return r * std::cos(theta);
Expand Down
8 changes: 4 additions & 4 deletions src/internal_modules/roc_core/fast_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ uint32_t fast_random_32();
//! Not cryptographically secure.
uint64_t fast_random_64();

//! Get random 64-bit float in range [0; 1].
//! Get random 32-bit float in range [0; 1].
//! Thread-safe and lock-free.
//! Uniformly distributed.
//! Not cryptographically secure.
double fast_random_float();
float fast_random_float();

//! Get random 64-bit integer in range [from; to].
//! Thread-safe and lock-free.
//! Uniformly distributed.
//! Not cryptographically secure.
uint64_t fast_random_range(uint64_t from, uint64_t to);

//! Get random 64-bit float with standard normal distribution.
//! Get random 32-bit float with standard normal distribution.
//! Thread-safe and lock-free.
//! Gaussian distribution N(0,1).
//! Not cryptographically secure.
double fast_random_gaussian();
float fast_random_gaussian();

} // namespace core
} // namespace roc
Expand Down
2 changes: 1 addition & 1 deletion src/tests/roc_core/test_fast_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST(fast_random, range_loop) {

TEST(fast_random, float_loop) {
for (int i = 0; i < 10000; i++) {
double res = fast_random_float();
float res = fast_random_float();

CHECK(res >= 0);
CHECK(res <= 1);
Expand Down
4 changes: 2 additions & 2 deletions src/tests/roc_stat/test_mov_quantile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ TEST(mov_quantile, stress_test) {

for (size_t i = 0; i < NumIterations; i++) {
const size_t q_win_sz = core::fast_random_range(MinWindow, MaxWindow);
const double q = core::fast_random_float();
const double q = (double)core::fast_random_float();

MovQuantile<double> quant(arena, q_win_sz, q);
CHECK(quant.is_valid());

double elems[NumElems] = {};

for (size_t n = 0; n < NumElems; n++) {
elems[n] = core::fast_random_float();
elems[n] = (double)core::fast_random_float();
quant.add(elems[n]);

const size_t n_elems = n + 1;
Expand Down

0 comments on commit b9d072d

Please sign in to comment.