Skip to content

Commit

Permalink
BugFix: AgentRandom::uniform<double>() would return range (0, 1]
Browse files Browse the repository at this point in the history
This is technically a breaking change, however only so much that epsilon is shifted.
  • Loading branch information
Robadob committed Feb 10, 2023
1 parent e91607f commit ae800a3
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/flamegpu/runtime/random/AgentRandom.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ __forceinline__ __device__ float AgentRandom::uniform() const {
}
template<>
__forceinline__ __device__ double AgentRandom::uniform() const {
return curand_uniform_double(d_random_state);
// curand naturally generates the range (0, 1], we want [0, 1)
// Conversion of High-Period Random Numbers to Floating Point - Jurgen A Doornik
// Based on: https://www.doornik.com/research/randomdouble.pdf
const uint32_t iRan1 = curand(d_random_state);
const uint32_t iRan2 = curand(d_random_state);
constexpr double M_RAN_INVM32 = 2.32830643653869628906e-010;
constexpr double M_RAN_INVM52 = 2.22044604925031308085e-016;
return (static_cast<int>(iRan1)*M_RAN_INVM32 + (0.5 + M_RAN_INVM52 / 2) + static_cast<int>((iRan2) & 0x000FFFFF) * M_RAN_INVM52);
}

/**
Expand Down

0 comments on commit ae800a3

Please sign in to comment.