gpt4 book ai didi

c 时间上的随机数

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:30 25 4
gpt4 key购买 nike

为什么这个程序只生成 0.1-0.2 的数字。它需要从 0-1 生成数字。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (int argc, char *argv[])
{
/*declare pseudo-random number */
float random;
srand(time(NULL));

random = (float) rand() / (float) RAND_MAX;
printf("The random number is %f\n", random);

return 0;
}

我在 mac 上使用 gcc 进行编译。

最佳答案

代码对我来说似乎没问题。尝试生成多个数字,然后查看:

for(i=1; i<100; i++){
random = (float) rand() / (float) RAND_MAX;
printf("%f\n", random);
}

关于c 时间上的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13000119/

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