/* This is a free Program, You can modify or redistribute it under the terms of GNU *Description: 给定一定的范围和定时,如给定1000,定时为2s,输出到2s时产生的1000之内的随机数
*Language: C++ *Development Environment: VC6.0 *Author: Wangzhicheng *E-mail: 2363702560@qq.com *Date: 2012/10/6*/ #include <iostream> #include <ctime> using namespace std; class ShowRandom { private: long Rang; //显示随机数的范围 int duration; //持续时间 void Producer() { //显示随机数,在持续时间到来时终止 clock_t start; clock_t end; int i; long n; srand(unsigned(time(0))); start=clock(); //获取此段代码开始时间 while(true) { n=rand()%Rang; cout<<n; system("cls"); for(int i=0;i<10000000;i++) ; //暂停一会 end=clock(); if(duration==int(end- start)/CLOCKS_PER_SEC) break; //计算出持续时间 } cout<<duration<<"秒之后产生的随机数是:"<<n<<endl; } public: ShowRandom(long r,double d) { setRang(r); setDuration(d); Producer(); } void setRang(long r) { Rang=r; } long getRang() const { return Rang; } void setDuration(int d) { duration=d; } long getDuration() const { return duration; } }; void main() { ShowRandom(10000,60); }