In java random number can get like
protected final static Random RANDOM = new Random(System.currentTimeMillis());
In c++ using GMP Library how it possible to generate random number?
I used code like
gmp_randstate_t s;
unsigned long seed;
seed = time(NULL);
gmp_randinit_default(s);
gmp_randseed_ui(s, seed);
mpz_class ran;
gmp_randclass rr(s);
ran =rr.get_z_bits(125);
long int random=ran.get_ui();
But i dont get random number.
Please help me.
First, there is no
gmp_randclassconstructor that takes agmp_randstateinstance, so your code didn’t compile for me. The recommended way to construct agmp_randclass instanceis usinggmp_randinit_default, like this:The first part of your code is seeding the
gmp_randstate_t s, but that random state variablesis not used in the second part of your code (after the above change). Unless seeded otherwise, the default GMP random number generator always starts with the same seed, which means the same sequence of random numbers will be generated each time you run the program. You can seed an instance ofgmp_randclassusing thegmp_randclass::seedfunction.The following code is similar to yours but seeds the random number generator based on the current time.
Note that as discussed in Random State Seeding, using a low-resolution current time is usually a poor choice for a random number generator seed.