Random number generation

Décio Luiz Gazzoni Filho decio at decpp.net
Thu Nov 3 14:27:28 CET 2005

On Nov 3, 2005, at 7:33 AM, Gabriele Martino wrote:

> Hi,
> I need to generate random numbers of 512 bits.
> I use the following code :
> mpz_t p;
> gmp_randstate_t rstate;
> mpz_init2(p,512);
> gmp_randinit(rstate,GMP_RAND_ALG_LC, 32);
> mpz_urandomb(p, rstate, 512);
> The numbers are generated but the problem is that if I run several  
> times this code it produces the SAME sequence of random numbers.
> How can I solve this problem?

I know I shouldn't try to help, but I will anyway. Be warned though  
that if you don't pay attention to my reply I won't provide any  
further help.

A pseudo-random number generator (such as the one that GMP uses)  
will, given an initial state, generate the same sequence always. The  
point is that you can use a small piece of information (the seed) to  
vary the initial state. You could use /dev/random to generate the  
seed, or perhaps read your CPU's cycle counter or other fine-grained  
timing source. After obtaining a seed, you can use the functions from  
here: http://www.swox.com/gmp/manual/Random-State-Seeding.html to  
seed the pseudo-random number generator.

I see that you're trying to generate 512-bit integers. That suggests  
an implementation of say the RSA cryptosystem. In that case, *don't*  
use the GMP pseudo-random number generator as it's not  
cryptographically safe (read: your system will be much much easier to  
crack than you think). In that case, use a real source of entropy  
such as /dev/random (on a Unix system, not sure what's the equivalent  
on a Windows system).

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://gmplib.org/list-archives/gmp-discuss/attachments/20051103/f3367c88/PGP.bin

More information about the gmp-discuss mailing list