简体   繁体   English

C ++ GMP生成随机数

[英]C++ GMP Generating Random Number

I'm trying to generate a huge random number in C++ using the GMP library but am having issues figuring out the syntax. 我正在尝试使用GMP库在C ++中生成一个巨大的随机数,但是在弄清楚语法时遇到了问题。 This is slightly different from other examples I've found because I need to set a floor and ceiling for the random number to be between. 这与我发现的其他示例略有不同,因为我需要为下一个随机数设置下限和上限。 This is kinda what I need to do: 这是我需要做的:

mpz_class high, low;

low  = pow(2,199);
high = pow(2,210);

// code to use the high and low numbers to generate the random number

I know this isn't much to go on, but again, I'm not sure what the syntax would even be at this point, I've tried several things but nothing I've found enables me to tell GMP to use a high and low range for the number generation. 我知道这没什么大不了的,但是再次,我不确定这时的语法是什么,我已经尝试了几件事,但是没有发现让我告诉GMP使用高和低范围的数字生成。

Thoughts? 有什么想法吗?

Using the logic presented by @Less, I wrote the following to solve my problem: 使用@Less提出的逻辑,我编写了以下代码来解决我的问题:

void 
makeprime ()
{
    // *********************** VARIABLE DECLARATION *********************** //
    // initilize the variables as gmp class instances
    mpz_t l, rand;
    unsigned long seed;
    // perform inits to create variable pointers with 0 value
    mpz_inits(l, rand);
    //mpz_init(rand);

    // calculate the random number floor
    mpz_ui_pow_ui(l, 2, 199);

    // initilze the state object for the random generator functions
    gmp_randstate_t rstate;
    // initialize state for a Mersenne Twister algorithm. This algorithm is fast and has good randomness properties.
    gmp_randinit_mt(rstate);

    // create the generator seed for the random engine to reference 
    gmp_randseed_ui(rstate, seed);

    /*
    Function:
    int mpz_probab_prime_p (const mpz_t n, int reps)

    Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), 
    or return 0 if n is definitely composite.
    */
    do {
        // return a uniformly distributed random number in the range 0 to n-1, inclusive.
        mpz_urandomb(rand, rstate, 310);

        // add the random number to the low number, which will make sure the random number is between the low and high ranges
        mpz_add(rand, rand, l);

        gmp_printf("randomly generated number: %Zd\n", rand);

    } while ( !(mpz_probab_prime_p(rand, 25)) );        

    // *********************** GARBAGE COLLECTION *********************** //
    // empty the memory location for the random generator state
    gmp_randclear(rstate);
    // clear the memory locations for the variables used to avoid leaks
    mpz_clear(l);
    mpz_clear(rand);
}

Thanks @Less for your logic and help! 感谢@Less的逻辑和帮助!

From Gmp Lib Documentation Gmp Lib文档

Function: void mpz_urandomb (mpz_t rop, gmp_randstate_t state, mp_bitcnt_t n) 函数:void mpz_urandomb(mpz_t rop,gmp_randstate_t状态,mp_bitcnt_t n)

Generate a uniformly distributed random integer in the range 0 to 2^n−1, inclusive 生成范围为0到2 ^ n-1(含)的均匀分布的随机整数

So, take 210 - 199, and use that as n, generate a random number and add the result to pow(2,199). 因此,取210-199,并将其用作n,生成一个随机数并将结果添加到pow(2,199)。

If you want something more granular than a power of 2 upper limit, this won't work for you. 如果您想要的东西比2的幂次方更细,则此方法对您不起作用。 You could try the unsigned int sized random function using the same technique above: 您可以使用上述相同的方法尝试使用无符号int大小的随机函数:

— Function: unsigned long gmp_urandomm_ui (gmp_randstate_t state, unsigned long n) —功能:无符号长gmp_urandomm_ui(gmp_randstate_t状态,无符号长n)

Return a uniformly distributed random number in the range 0 to n-1, inclusive. 返回范围为0到n-1(包括0和n-1)的均匀分布的随机数。

Here, you would find your granular range and use that for n. 在这里,您将找到自己的粒度范围,并将其用于n。 Then add the random number to your lower value. 然后将随机数添加到较低的值。 The limitation is n must be less than MAXUINT, usually 2^32 -1 限制是n必须小于MAXUINT,通常为2 ^ 32 -1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM