How to generate Random N bit hex numbers?

Sam Rawlins sam.rawlins at gmail.com
Mon Apr 19 19:37:07 CEST 2010


On Mon, Apr 19, 2010 at 7:38 AM, Ali Sydney <asydney at k-state.edu> wrote:

> All,
>    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.
>

GMP stores integers as mpz_t types, not specifically as binary, decimal, or
hexedecimal. I think you want to use mpz_urandomb(rop, state, n) where you
will generate an n-bit pseudo random number, just like you want. To convert
it to a hexadecimal string, use mpz_get_str(*str, 16, op).

If you actually want to generate a pseudo random number with n _digits_ in
hexadecimal, then instead use 4n in mpz_urandomb(). A number with 4n binary
digits has n hex digits.


>
> If this is not possible, how would I go about converting an mpz_class
> integer to hexadecimal? For example if
>
> mpz_class z;
> z=10;
>
> how would I then convert z to hexadecimal?
>
> Best Regards,
> Ali
>
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-discuss
>



-- 
Sam Rawlins


More information about the gmp-discuss mailing list