How to generate Random N bit hex numbers?

Ali Sydney asydney at
Mon Apr 19 16:38:08 CEST 2010

    I am rather new to the GMP. With this said, I am attempting to generate a random N bit (where N can be any integer ex. 1024) hexadecimal number using the c++ interface.  Below is a simple test where I generate a random '6' bit number in decimal.  

gmp_randclass r (gmp_randinit_lc_2exp_size, 32);
mpz_class z; 
z=r.get_z_bits(6);  //z stores value of random integer.

However, I have not been able to find a function to do this similar process and produce hexadecimal random number.  

If this is not possible, how would I go about converting an mpz_class integer to hexadecimal? For example if

mpz_class z;

how would I then convert z to hexadecimal?

Best Regards,

More information about the gmp-discuss mailing list