gpt4 book ai didi

c - 如何向 ISAAC 随机数生成器提供种子

转载 作者:行者123 更新时间:2023-11-30 15:10:07 25 4
gpt4 key购买 nike

我有以下代码来使用 ISAAC 生成随机数:

int main()
{
/* Initialize the structure to 0 */
randctx ctx;
ctx.randa = ctx.randb = ctx.randc = (ub4)0;

/* Initialize the seed */
for (ub4 i=0; i<256; ++i) {
ctx.randrsl[i] = i;
}

/* Initialize the random numbers from the seed */
randinit(&ctx, TRUE);

printf("%.8lx\n", rand(&ctx));
}

我从 How to use ISAAC in C 得到了上面的代码

我还有一个代码可以从/dev/random 读取以获取种子:

int myFile = open("/dev/random", O_RDONLY);            
uint32_t rand;
uint32_t randomNum = read(myFile, &rand, sizeof(rand)) ;
printf(" %u \n", rand);
close(myFile);

如何向 ISAAC 提供种子(即兰特)?

谢谢

最佳答案

TRUE 传递给 randinit 标志时,种子取自 ctx.randrsl。但我建议使用 RANDSIZsizeof(ctx.randrsl) 而不是硬编码其值 256。

所以:

ssize_t bytes_read = read(myFile, &ctx.randrsl, sizeof(ctx.randrsl));
if(bytes_read != sizeof(ctx.randrsl)){ printf("cannot initialize seed\n"); exit(1); }

关于c - 如何向 ISAAC 随机数生成器提供种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36382040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com