求助!一个能产生随机数的C++程序

来源:百度知道 编辑:UC知道 时间:2024/05/23 13:57:46
对于从1到35的数,能够随机产生5个数,并且这5个数不相同!谢谢了!

#include <iostream>
#include <ctime>
using namespace std;
int getRand()
{
static int ctrl = 0;
if(!ctrl)
{
srand((unsigned)time(0));
ctrl++;
}
return rand()%35+1;
}
int main()
{
int a[5] = {0};
int num = 0;
for(int i=0;i<5;i++)
{
a[i] = getRand();
for(int j=0;j<i;j++)
{
if(a[j] == a[i])
{
a[i]++;
if(a[i] >35)
a[i] = 1;
}
}
cout<<a[i]<<" ";
}
}

其实这样只能在一定程度上避免错误,我现在还没有研究出完全没有漏洞的方法。对不住了。

rand()%35,如果觉得不够随机
用srand