I would like to generate a random integer z of n bytes such that
2^(n-1) <= z <= 2^n - 1
Whenever I run the following code gmp spits out the same exact integer, what am I doing wrong?
// Random int of n bits.
const auto n_bits = 1024;
mpz_t mpz_int;
gmp_randstate_t state;
mpz_init(mpz_int);
gmp_randinit_default(state);
mpz_rrandomb(mpz_int, state, n_bits);
std::cout<<"Random int generated:"<<std::endl;
std::cout<<mpz_get_str(nullptr, 10, mpz_int)<<std::endl;
The output of mpz_rrandomb
changes only when I change the n_bits
parameter.
I tried this on Ubuntu and MacOS.. I also tried mpz_urandomb
-- same problem. I assume I am missing some initializations, I've been going over gmp documentation for hours and I can't find a reason why the above wouldn't work.
To reproduce, stuff the above code into the main function, compile with flags:
g++ main.cpp -O2 -Wall -std=c++14 -lstdc++ -lgmp -lgmpxx
Thank You.