mpz_urandomb 总是给出相同的结果

mpz_urandomb always giving same result

无论何时执行这段代码,它都会给出相同的结果。我无法获得随机值。

temp 的值始终为 6400306986398558324。(lambda = 256。)

mpz_t group_size,temp;
mpz_init(group_size);
mpz_init(temp);
gmp_randstate_t state;
gmp_randinit_mt (state);
mpz_urandomb (temp, state,lambda);
mpz_nextprime (group_size, temp);

printf("temp : %ld \n",mpz_get_ui(temp));
printf("Group Size : %ld \n",mpz_get_ui(group_size));

您需要为状态设置种子。来自 official documentation:

Function: void gmp_randseed (gmp_randstate_t state, const mpz_t seed)

Function: void gmp_randseed_ui (gmp_randstate_t state, unsigned long int seed)

这是您的代码示例(在现实世界中并不是很有用,因为 time(NULL) 将 return 整整一秒的结果相同)。

...
gmp_randinit_mt (state);
gmp_randseed_ui(state, time(NULL))
mpz_urandomb (temp, state,lambda);
...