std:: rand
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<cstdlib>
|
||
|
int
rand
(
)
;
|
||
返回一个来自范围
[
0
,
RAND_MAX
]
的伪随机整数值。
std::srand()
用于为
rand()
所使用的伪随机数生成器设置种子。
如果在调用
std::srand()
之前使用了
rand()
,则
rand()
的行为会如同已通过
std::
srand
(
1
)
设置种子一般。
每次使用
std::srand()
为
rand()
设置种子后,该函数在后续调用中必须生成相同的数值序列。
标准库中的其他函数可能会调用
rand
。具体哪些函数会这样做是由实现定义的。
rand() 是否具有线程安全性是由实现定义的。
目录 |
返回值
介于 0 与 RAND_MAX 之间的伪随机整数值。
注释
无法保证所生成随机序列的质量。
过去,某些
rand()
的实现存在随机性、分布性和序列周期性的严重缺陷(在一个著名案例中,其最低位在调用时仅简单地交替于
1
和
0
之间)。
rand()
不推荐用于需要高质量随机数生成的场景。
建议使用 C++11 的
随机数生成
工具来替代
rand()
。
(since C++11)
示例
下面的
bounded_rand()
函数是
去偏模运算(一次性)
的适配版本。
#include <cstdlib> #include <ctime> #include <initializer_list> #include <iostream> unsigned bounded_rand(unsigned range) { for (unsigned x, r;;) if (x = rand(), r = x % range, x - r <= -range) return r; } int main() { std::srand(std::time({})); // 使用当前时间作为随机数生成器的种子 const int random_value = std::rand(); std::cout << "Random value on [0, " << RAND_MAX << "]: " << random_value << '\n'; for (const unsigned sides : {2, 4, 6, 8}) { std::cout << "Roll " << sides << "-sided die 8 times: "; for (int n = 8; n; --n) std::cout << 1 + bounded_rand(sides) << ' '; std::cout << '\n'; } }
可能的输出:
Random value on [0, 2147483647]: 948298199 Roll 2-sided die 8 times: 2 2 1 2 1 1 2 2 Roll 4-sided die 8 times: 1 3 4 2 1 3 3 1 Roll 6-sided die 8 times: 3 2 1 6 6 4 4 2 Roll 8-sided die 8 times: 4 5 6 6 3 6 1 2
参见
|
(C++11)
|
生成在范围内均匀分布的整数值
(类模板) |
|
初始化伪随机数生成器
(函数) |
|
|
std::rand
可生成的最大可能值
(宏常量) |
|
|
在指定范围内生成随机整数
(函数模板) |
|
|
C 文档
关于
rand
|
|