gpt4 book ai didi

c - 用 C 生成随机数

转载 作者:行者123 更新时间:2023-11-30 18:41:31 25 4
gpt4 key购买 nike

如果我想在 C 中生成 0 到 1,000,000 之间的不同随机数,以下内容是否足够?

srand(time(NULL));
int k = (rand() % 1000000;

从我看到的输出来看,只生成了大约 32767 个不同的随机数!

谢谢!

最佳答案

rand int rand (void); Generate random number Returns a pseudo-random integral number in the range between 0 and RAND_MAX.

RAND_MAX Maximum value returned by rand This macro expands to an integral constant expression whose value is the maximum value returned by the rand function.

This value is library-dependent, but is guaranteed to be at least 32767 on any standard library implementation.

关于c - 用 C 生成随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21043042/

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