题目:有5个数,假设是1到5,每次抽到的概率相等,每次抽到后都放回,编程实现
#include<iostream>
#include<stdlib.h>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand( (unsigned)time( NULL ) );
//srand(unsigned(time(0))); 这是原来的随机数产生语句,用上一行的替换了
int a[5] = {1,2,3,4,5};
int i = 1;
while(i<=10)
{
int b=rand()%5;
cout<< a[b] <<endl;
i++;
}
cin>>i;
return 0;
}
本文介绍了一个使用C++实现的简单程序,该程序能够从1到5中随机抽取数字,并且每次抽取后都将数字放回。通过使用srand和rand函数确保了每次运行程序时都能获得不同的随机数序列。
940

被折叠的 条评论
为什么被折叠?



