随机函数rand()的小程序:
#include<ioStream>
#include <TIMe.h>
#include <fstream>
using namespace std;
unsigned t[300],temp;
void creat_rand() //产生随机数的函数
{
long i=1;
cout<<"为您产生的随机数如下:"<<endl;
srand(time(0)); //用此函数设定种子值,使每次产生的随机数不一样
for(i=1;i<21;)
{
temp=(rand()%10000+1000);
if(temp>999 && temp<=9999)
{
t[i]=temp;
cout<<"第"<<i<<"个"<<t[i]<<" ";
if(i%5==0)
cout<<endl;
i++;
}
}
}
void Search_number(unsigned t[],int n)//查找函数
{
cout<<endl;
DeleteFile("randnumber.txt");
system("pause");
cout<<"后两位数字相等的随机数:"<<endl;
ofstream output("randnumber.txt",ios::out);
int cand1,cand2;
for(int i=1;i<n;i++)
{
cand1=t[i]%10;
cand2=t[i]%100/10;
if(cand1==cand2)
{
cout<<t[i]<<" ";
output<<t[i]<<" ";
}
}
output.close();
}
void main()//主函数
{
creat_rand();
search_number(t,20);
}