Ok so what if I took rand and instead of modding it by the number I want and adding one, I just added the result of a test to see whether rand() is bigger than half or less than half of the maximum number you can get of that type? I know it’s the most inefficient thing ever, but still I could work, right? Sorry I mean it might make a uniform distribution.
1 2 3 4 5 6 7 8 9
#include <cstdlib>
size_t udist(size_t maxVal){
size_t res = 0;
for(size_t i = 0;i < maxVal;i++){
res += (int)( (RAND_MAX/2) > rand() );
}
return res;
}
Yeah you can do bit masking to get """uniform"""" distributions, e.g. mask with (RAND_MAX + 1) / 2^m. In other words, "rand() % n is not uniformly distributed unless RAND_MAX+1 happens to be divisible by n."
But rand isn't even guaranteed to be uniform to begin with.