srand()就是给rand()提供种子seed
如果srand每次输入的数值是一样的,那么每次运行产生的随机数也是一样的,
srand(n)
for(10)
rand()
也就是说,以一个固定的数值作为种子是一个缺点。 通常的做法是 以这样一句代码srand((unsigned) time(NULL));来取代,这样将使得种子为一个不固定的数, 这样产生的随机数就不会每次执行都一样了。
1,先看一个例子
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */