现在的位置: 首页 > 综合 > 正文

C++ 随机生成不重复的随机数

2012年06月01日 ⁄ 综合 ⁄ 共 675字 ⁄ 字号 评论关闭

#include<stdio.h>
#include<iostream.h>
#include <stdlib.h>
#include <time.h>
#include<ctime>

#define MAXSIZE 10000

void test()
{
 int b[MAXSIZE];
 int count =0 ;

 srand(time(0));
 //  srand(time(NULL));
 printf("One hunderd random number from 0 to %d\n\n",MAXSIZE);
 // printf("Ten random numbers from 0 to 99\n\n");
 for( int i = 0; i < MAXSIZE; i++ )
 {
  b[i] = rand()%MAXSIZE;
  count ++;
  for( int j = 0; j < i; j++)
  {
   if( b[j] == b[i] )   
   {
    i--;   
    break;
   }
  }
 }
 for( int m = 0; m < MAXSIZE; m++ )
 {
  printf("%d\t" ,b[m]);
 }
 printf("total circle number is: %d\n",count);
}

int main()
{
 time_t begin,end;
 begin=clock();
 //begin test code
 test();
 //end test code
 end=clock();
 cout<<"runtime: "<<double(end-begin)/CLOCKS_PER_SEC<<endl;
 return 0;
}

抱歉!评论已关闭.