展会信息港展会大全

随机函数rand()的实例代码
来源:互联网   发布日期:2016-01-26 10:29:57   浏览:2470次  

导读:随机函数rand()的小程序: includeiostream include time h include fstream include windows h using namespace std; unsigned t[300],temp; void creat_rand() 产 ...

随机函数rand()的小程序:

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

}

赞助本站

人工智能实验室

相关热词: 随机函数 rand

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港