jeudi 4 juin 2015

Random number changing slowly

I made a function that generates a random prime between two values. The problem is that the number is not really random. The value changes, but after some time. Running the code will make clear what I mean.

#include <iostream>
#include <gmpxx.h>
#include <time.h>

mpz_class odd(mpz_class n0, mpz_class nf) {
    mpz_class odd, randd;
    gmp_randclass rr(gmp_randinit_default);
    rr.seed(time(NULL));
    randd = n0 + rr.get_z_range(nf-n0);
    randd = randd/2;
    odd = 2*randd + 1;
    return odd;
}


int main() {
    mpz_class n0("100000"), nf("10000000000");
    while(1) {
        std::cout << odd(n0, nf) << std::endl;
    }
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire