c - 用 C 生成随机数

标签 c random numbers

如果我想在 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/

相关文章:

c - 窗口服务: get each users' %APPDATA% variable

c - OpenCV - 从 slider 更新视频播放位置

jquery-selectors - 通过 jQuery 选择数字类型的输入

c - 从文本文件中读取数字并将其用作输入

c - 在哪里可以找到 ncurses C API 的完整引用?

java - 在对象实例之间随机共享?

c++ - 我应该如何正确播种 C++11 std::default_random_engine?

mysql - 在 MySQL 中创建具有随机名称的临时表

javascript - 为什么 parseInt(8,3) == NaN 和 parseInt(16,3) == 1?

c++ - 如何在C++中使用new分配内存字节数