随机数产生问题

#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

int main(int argc, char *argv[])
{
    clock_t start=clock();
    double spot;
   
    srand(time(0));//设定随机数种子
    int n=100; // 产生100个随机数
    int *p=new int[200];//开辟200个int空间
   
    for(int i=0;i<100;i++)//产生100个0-150之间互相不相同的随机数
    {
    p[i]=rand()%150;//产生0-150之间的值
    for(int k=0;k<i-1;k++)
     {if(p[i]==p[k])//每生成一个随机数,与之前的所有随机数比较,如果相同则重新产生随机数,同时跳出此次循环
     i--;
     break;}
    }
   
    for(int j=0;j<100;j++)  //打印输出所有产生的随机数
    {    cout<<*(p+j)  <<endl;}

     free(p);//释放指针
   
    clock_t end=clock();
    spot=double(end-start)/CLOCKS_PER_SEC;
    cout<<"运行时间是="<<spot<<"秒"<<endl;//计算运行时间
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

 

 

 

posted @ 2013-04-28 17:35  夜雨阑珊  阅读(216)  评论(0编辑  收藏  举报