您的位置:首页 > 其它

产生不重复随机数

2011-08-31 00:13 148 查看
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int aTest[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int nPos, nTemp;

srand((unsigned)time(NULL));

for(int nCount = 0; nCount < 10; nCount ++)
{
nPos = rand()%10;
nTemp = aTest[nPos];
aTest[nPos] = aTest[nCount];
aTest[nCount] = nTemp;

cout << aTest[nCount]<< endl;
}
return 0;
}

//基于交换位置,所以不可能重复
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: