std:: srand
| 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>
|
||
|
void
srand
(
unsigned
seed
)
;
|
||
使用值
seed
为
std::rand()
所使用的伪随机数生成器设置种子。
如果在任何调用
srand()
之前使用了
std::rand()
,
std::rand()
的行为将如同使用
srand
(
1
)
进行播种。
每次使用相同的
seed
为
std::rand()
设置种子时,它必须生成相同的数值序列。
srand()
不保证是线程安全的。
目录 |
参数
| seed | - | 种子值 |
返回值
(无)
注释
通常来说,伪随机数生成器应当在程序开始时、任何调用
rand()
之前仅被初始化一次。
不应重复初始化,也不应在每次希望生成新一批伪随机数时重新初始化。
标准做法是使用对 std:: time ( 0 ) 的调用结果作为种子值。 然而, std::time 返回的是 std:: time_t 类型的值,而 std:: time_t 并不保证是整数类型。 但在实际应用中,所有主流实现都将 std:: time_t 定义为整数类型,这也是POSIX标准所要求的。
示例
可能的输出:
Random value on [0, 2147483647]: 1373858591
参见
|
生成伪随机数
(函数) |
|
|
std::rand
可生成的最大值
(宏常量) |
|
|
重新播种每线程随机引擎
(函数) |
|
|
C 文档
关于
srand
|
|