rand()与srand()

1.不用srand()的话
两次运行程序产生的随机数序列相同

2.用srand()

两次运行程序产生的随机数则不同


示例程序:

#include<iostream>
#include<cstdio>
#include<cstdlib> // rand()、srand() 
#include<ctime> // time()
using namespace std;
 
int main(){
     int i;
     srand(time(NULL)); // 不加此句的话,两次运行产生的随机数序列相同 
     for(i=0;i<10;i++){
     	printf("%d ",rand()%100);
     }
     return 0;
}


posted @ 2015-03-14 19:26  gongpixin  阅读(252)  评论(0编辑  收藏  举报