This can done this way:
int clamp(int rnd, int min, int max)
{
return min + rnd % (max - min + 1);
}
Can this be done without using division? Returned value doesn't necessarily have to match, however, uniform distribution has to be preserved.
rnd
, min
, and max
are arbitrary integers. rnd
in my case is a random integer.
One possible solution is to convert to double
in range of [0...1)
using std::ldexp and then multiply, but I'd like to know if there is a simpler solution that doesn't involve floats and/or division.