mpz_rrandomb most significant bit always set

Torbjörn Granlund tg at
Sat Mar 5 17:54:09 UTC 2016

Emmanuel Thomé <emmanuel.thome at> writes:

  The documentation of mpz_rrandomb says:
   -- Function: void mpz_rrandomb (mpz_t ROP, gmp_randstate_t STATE,
            mp_bitcnt_t N)
       Generate a random integer with long strings of zeros and ones in
       the binary representation.  Useful for testing functions and
       algorithms, since this kind of random numbers have proven to be
       more likely to trigger corner-case bugs.  The random number will
       be in the range 0 to 2^N-1, inclusive.
  I was surprised to notice that in fact, the most significant bit of
  the output seems to be always set, as shown by the code below.
  Sure, this behaviour does not *contradict* the documentation, but
  perhaps the latter could be changed to reflect this fact, as for
  example in:
       The random number will be in the range 2^(N-1) to 2^N-1, inclusive.
Thanks for spotting this.

I agree with your change, but I think our claim that 0 is included in
the returned numbers is wrong.  A less helpful fix would be to remove
the word "inclusive"...  :-)

Please encrypt, key id 0xC8601622

More information about the gmp-discuss mailing list