0
mpz_t rand1,rand2;
mpz_init (rand1);
mpz_init (rand2);

mpz_random (rand1,512);
//mpz_random (rand2,512);

mpz_nextprime ( rand2, rand1 );
gmp_printf("random %Zd\n", rand2);
//free the big ints
mpz_clear(rand1);
mpz_clear(rand2);

However is i print after random it works but the second i call next prime nothing prints out?

csestony
  • 215
  • 2
  • 12

1 Answers1

1

From the info page:

-- Function: void mpz_random (mpz_t ROP, mp_size_t MAX_SIZE) Generate a random integer of at most MAX_SIZE limbs. The generated random number doesn't satisfy any particular requirements of randomness. Negative random numbers are generated when MAX_SIZE is negative.

This function is obsolete. Use mpz_urandomb' ormpz_urandomm' instead.

You're creating a random(ish) integer with 512 limbs, that's 16384 bits if a limb is 32 bits, or 32768 bits if a limb is 64 bits. Primes in that range are sparse, so GMP will take a while to find one.

Presumably you intended to find a prime of 512 bits?

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • That depends, are you on a 64-bit system or 32? That would be either 8 or 16 limbs. I apologise for the original snark, had a bad moment. – Daniel Fischer Oct 06 '12 at 21:45
  • @Thefirstkilla But, the random integer generated by `mpz_random` can be shorter than the specified number of limbs, so you should better specify one limb more and check whether it's large enough but not too large. If not, create a new random integer and check again. – Daniel Fischer Oct 06 '12 at 21:50
  • 32 bit thanks i tried to use the urandomb but the numbers were too small – csestony Oct 06 '12 at 21:51
  • mpz_urandomb(rand_Num,r_state,14); returned small numbers – csestony Oct 06 '12 at 21:52
  • I would recommend creating a random integer in the range `0` to `2^511-1` with `urandomb` (so the mp_bitcnt_t parameter should be 511), and add that to `2^511` to get a 512-bit random integer. – Daniel Fischer Oct 06 '12 at 21:53
  • http://gmplib.org/manual/Nomenclature-and-Types.html found the man page thanks for everything – csestony Oct 06 '12 at 21:53
  • great ok well got it trying it now – csestony Oct 06 '12 at 21:54
  • Finally i Can move on got it Dan thanks for the help Faster response than my teacher – csestony Oct 06 '12 at 21:58