C++ GMP 生成随机数
C++ GMP Generating Random Number
我正在尝试使用 GMP 库在 C++ 中生成一个巨大的随机数,但在弄清楚语法时遇到了问题。这与我发现的其他示例略有不同,因为我需要为随机数设置下限和上限。这有点像我需要做的:
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
我知道这没什么好继续的,但是我再次不确定此时的语法是什么,我已经尝试了几种方法,但我发现的任何东西都无法告诉我GMP 使用高低范围生成数字。
想法?
Function: void mpz_urandomb (mpz_t rop, gmp_randstate_t state,
mp_bitcnt_t n)
Generate a uniformly distributed random integer in the range 0 to
2^n−1, inclusive
因此,取 210 - 199,并将其用作 n,生成一个随机数并将结果添加到 pow(2,199)。
如果你想要比 2 的幂上限更细化的东西,这对你不起作用。您可以使用与上述相同的技术尝试 unsigned int 大小的随机函数:
— Function: unsigned long gmp_urandomm_ui (gmp_randstate_t state, unsigned long n)
Return a uniformly distributed random number in the range 0 to n-1, inclusive.
在这里,您将找到您的粒度范围并将其用于 n。然后将随机数添加到您的较低值。限制是n必须小于MAXUINT,通常是2^32 -1
使用@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);
}
感谢@Less 的逻辑和帮助!
我正在尝试使用 GMP 库在 C++ 中生成一个巨大的随机数,但在弄清楚语法时遇到了问题。这与我发现的其他示例略有不同,因为我需要为随机数设置下限和上限。这有点像我需要做的:
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
我知道这没什么好继续的,但是我再次不确定此时的语法是什么,我已经尝试了几种方法,但我发现的任何东西都无法告诉我GMP 使用高低范围生成数字。
想法?
Function: void mpz_urandomb (mpz_t rop, gmp_randstate_t state, mp_bitcnt_t n)
Generate a uniformly distributed random integer in the range 0 to 2^n−1, inclusive
因此,取 210 - 199,并将其用作 n,生成一个随机数并将结果添加到 pow(2,199)。
如果你想要比 2 的幂上限更细化的东西,这对你不起作用。您可以使用与上述相同的技术尝试 unsigned int 大小的随机函数:
— Function: unsigned long gmp_urandomm_ui (gmp_randstate_t state, unsigned long n)
Return a uniformly distributed random number in the range 0 to n-1, inclusive.
在这里,您将找到您的粒度范围并将其用于 n。然后将随机数添加到您的较低值。限制是n必须小于MAXUINT,通常是2^32 -1
使用@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);
}
感谢@Less 的逻辑和帮助!