Using bitwise & instead of modulus operator to randomly sample integers from a range
问题 I need to randomly sample from a uniform distribution of integers over the interval [LB,UB] in C++. To do so, I start with a "good" RN generator (from Numerical Recipes 3rd ed.) that uniformly randomly samples 64-bit integers; let's call it int64() . Using the mod operator, I can sample from the integers in [LB,UB] by: LB+int64()%(UB-LB+1); The only issue with using the mod operator is the slowness of the integer division. So, I then tried the method suggested here, which is: LB + (int64()&