srand()函数
Sets a random starting point.
void srand( unsigned int seed );
Parameters
seed
Seed for random-number generation
Remarks
The srand function sets the starting point for generating a series of pseudorandom integers. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. rand retrieves the pseudorandom numbers that are generated. Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.
Requirements
Routine | Required header |
srand | <stdlib.h> |
注:不使用srand()而直接使用rand()函数和用srand(1)设置种子的效果一样,rand()将会返回任何时候都一样的非随机数,这显然是我们不想看到的。
1) 首先给srand()提供一个种子,它是一个unsigned int类型,其取值范围从0~65535;
2) 然后调用rand(),它会根据提供给srand()的种子值返回一个随机数(在0到32767之间)
给出一段代码,很简单,一看就懂:





































