{
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语言随机函数的写法的信息别忘了在本站进行查找喔 。
推荐阅读
- 餐饮直播手稿素材,餐饮店直播介绍
- 电视遥控开关怎么关闭,电视遥控器怎么关电视
- 有哪些网络游戏好玩,有什么网络游戏好玩的
- 虚拟机终端操作用python运行,虚拟机终端操作用python运行程序
- 这样学java不枯燥代码 怎么学java不走弯路
- 无线路由器密码怎么破,无线路由器密码怎么破解视频
- 铁岭虚拟主机办理流程,虚拟主机办公
- mysql怎么增加密码 如何给mysql加锁
- jquery实现div动画显示,jquery实现动画效果的方法