mpz_urandomm function return the same value

Henno Brandsma hennobrandsma at
Wed Sep 21 19:41:46 CEST 2011

Op 21 sep. 2011, om 17:27 heeft Creatxr het volgende geschreven:

> Hi,
> could somebody tell me why it always the same value with function
> "mpz_unrandmm".
> thanks.
> ----------------------------------------------------------------
>    mpz_t rop, n;
>    gmp_randstate_t state;
>    mpz_init(rop); mpz_init(n);
>    gmp_randinit_default(state);

You initialize it but then you do not seed it: this means you probably will always generate the same sequence, as you have seen.
So use gmp_randseed(state, (mpz_t) my_seed), or gmp_randseed_ui(state, (long) my_seed)
as well, before you use mpz_urandomm
How you get the seed: use system data, time, etc. Don't use for cryptographic purposes if your seed is predictable…


Henno Brandsma 

>    mpz_set_ui(n, o);
>    mpz_urandomm(rop, state, n);   ////////////////// rop return the same value
>    r.setValue(rop); //////////////////set rop the r object field, rop
> is always the same value
>    gmp_randclear(state);
>    mpz_clear(rop); mpz_clear(n);
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at

More information about the gmp-discuss mailing list