首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

如何样让程序每次运行,产生不同的随机数

2012-02-27 
怎么样让程序每次运行,产生不同的随机数#include stdafx.h#include windows.h#include stdlib.h#inc

怎么样让程序每次运行,产生不同的随机数
#include "stdafx.h"
#include "windows.h"
#include "stdlib.h"
#include <iostream>

using namespace std; 

int _tmain(int argc, _TCHAR* argv[])
{
int (*a)[3]=new int[2][3];
int i,j,m,n,k,Ran;

for(j=0;j<=3;j++)
{
Ran=rand();
m=Ran%2;
n=(1-m)*(Ran%2);
k=1-m-n;
a[0][j]=m;
a[1][j]=n;
a[2][j]=k;
cout<<"产生的随机数为"<<Ran<<endl;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=3;j++)
{
cout<<a[i][j];
}
cout<<endl;
}


Sleep(30000);
delete a;
return 0;
}


这个产生随机数的代码,每次运行时都是相同的。


[解决办法]
开始加上

C/C++ code
 /* initialize random seed: */ srand ( time(NULL) ); 

热点排行