c语言随机函数的写法 c语言随机函数生成随机数( 三 )


{
int j = 0;
number[i] = rand() % n;//产生随机数
while (1)
{
if (number[i] == number[j])//若有相同则继续循环重新安排随机数
{
number[i] = rand() % n;//产生随机数
j = 0;//若遇到相同的就从头遍历
continue;
}
if (j == (i - 1))//若遍历完就跳出
break;
j++;
}
coutnumber[i]" ";
}
coutendl;
return 0;
}
程序运行结果如下:
扩展资料:
利用vector进行随机数输出:
#include iostream
#include vector
#include time.h
using namespace std;
int main()
{
const int n = 10;
int randnum;
vectorint number;
for (int i = 0; in; i++)
{
number.push_back(i + 1);//从尾部添加元素
coutnumber[i]" ";
}
coutendl;
srand((unsigned)time(NULL));
for (int j = 0; jn; j++)//其余随机数循环产生
{
randnum = rand() % (n - j);//rand函数生成的随机数是0-(n-1)
coutnumber.at(randnum)" ";
number.erase(number.begin() + randnum);
}
coutendl;
return 0;
}
c语言随机函数的写法的介绍就聊到这里吧 , 感谢你花时间阅读本站内容 , 更多关于c语言随机函数生成随机数、c语言随机函数的写法的信息别忘了在本站进行查找喔 。

推荐阅读