随机函数rand()小程序
来源:优易学  2011-12-15 17:58:12   【优易学:中国教育考试门户网】   资料下载   IT书店

  #include<iostream>

  #include <time.h>

  #include <fstream>

  #include <windows.h>

  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);

  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训