不重复随机序列

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

int random(int a,int b)
{
srand(NULL);
return rand()%(b-a)+a;
}

void exchange(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}

int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int length=sizeof(a)/sizeof(int);
for (int i=0;i<length;i++)
{
exchange(a[i],a[random(i,length)]);
}

for (int i=0;i<length;i++)
{
cout<<a[i]<<"";
}
system("pause");
return 0;
}
posted @ 2012-02-17 19:00  Dsp Tian  阅读(549)  评论(0编辑  收藏  举报