注册 登录
编程论坛 C++教室

有個关于srand的问题

julian1209 发布于 2008-05-07 12:15, 759 次点击
又两个小程序:
   第一个:#include <iostream>
#include "stdlib.h"
#include "time.h"
using namespace std;

int getrand(){
    srand(time(NULL));
    return rand()%1000+1;   
}

void main(){
    int t;
    cout<<"随机产生的20个数显示如下:"<<endl;
    for(int i=0;i<20;i++){
        t=getrand();
        cout<<t<<endl;
    }
}

  第二个:#include <iostream>
#include "stdlib.h"
#include "time.h"
using namespace std;

int getrand(){
   
    return rand()%1000+1;   
}

void main(){
    int t;
    srand(time(NULL));
    cout<<"随机产生的20个数显示如下:"<<endl;
    for(int i=0;i<20;i++){
        t=getrand();
        cout<<t<<endl;
    }
}

为什么第一个函数产生的随机数是一样的,而第二个不一样?
srand()放在main函数里面和放在其外面有什么区别吗?
2 回复
#2
newyj2008-05-07 12:59
好象是在调用完 函数后  函数会释放掉
不知道 是不是这个
#3
julian12092008-05-07 18:05
呵呵
還是沒明白
1