Browse Source

Use more precise conversion from uint64_t -> float on [0,1) for `random::uniform()`.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
585de0d318
1 changed files with 2 additions and 2 deletions
  1. +2
    -2
      src/random.cpp

+ 2
- 2
src/random.cpp View File

@@ -57,8 +57,8 @@ uint64_t u64() {
}

float uniform() {
// Make sure to only use 24 bits so it's impossible to round up to 1.f.
return (xoroshiro128plus_next() >> (64 - 24)) / std::pow(2.f, 24);
// The multiplier is 2f7fffff in hex. This gives maximum precision of uint32_t -> float conversion and its image is [0, 1).
return u32() * 2.32830629e-10f;
}

float normal() {


Loading…
Cancel
Save