From d0968a9c52019b817c001562adbb875780d94786 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Thu, 17 Mar 2022 12:48:41 +0100 Subject: BLI: add probabilistic rounding utility --- source/blender/blenlib/intern/rand.cc | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'source/blender/blenlib/intern') diff --git a/source/blender/blenlib/intern/rand.cc b/source/blender/blenlib/intern/rand.cc index 27774cad31b..17bf5585f3f 100644 --- a/source/blender/blenlib/intern/rand.cc +++ b/source/blender/blenlib/intern/rand.cc @@ -374,6 +374,15 @@ void RandomNumberGenerator::seed_random(uint32_t seed) this->seed(seed + hash[seed & 255]); } +int RandomNumberGenerator::round_probabilistic(float x) +{ + /* Support for negative values can be added when necessary. */ + BLI_assert(x >= 0.0f); + const float round_up_probability = fractf(x); + const bool round_up = round_up_probability > this->get_float(); + return (int)x + (int)round_up; +} + float2 RandomNumberGenerator::get_unit_float2() { float a = (float)(M_PI * 2.0) * this->get_float(); -- cgit v1.2.3