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

发生某一范围的随机数

2012-10-21 
产生某一范围的随机数/*This is a free Program, You can modify or redistribute it under the terms of

产生某一范围的随机数

/*This is a free Program, You can modify or redistribute it under the terms of GNU*Description: 随机数发生器,指定一个数,比如1000,要求随机产生1到1000之间的             任意数,并且1到1000之间任何一个数产生的概率是相等的,都是1/1000*Language: C++*Development Environment: VC6.0*Author: Wangzhicheng*E-mail: 2363702560@qq.com*Date: */#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);}
 

 

 


 

热点排行