generate a random number whose reminder modulo t is given, without division

  Kiến thức lập trình

Suppose we have a random number generator, returning a $n$-bit integer (e.g. uint64_t) I wish to manipulate the number so that it will return a uniform random $n$-bit integer, but with a reminder $r$ modulo $t$.

Given that the random number is $x$. One way to achieve it is to do $$foor(x/t)cdot t +r$$, but this uses a long division, which might be ineffective slowing down the generator.

What can I do?

LEAVE A COMMENT