«

C++随机数应用实例代码

点亮灯 发布于 阅读:36 C++


#include<iostream>  //引入iostream库,才能使用输入输出流,cin和cout
#include<cstdlib>   //工具函数,包括内存管理、随机数生成、程序控制等
#include<ctime>     //提供与时间相关的函数和类型
using namespace std;    //引入 std 命名空间

int main()
{
//1,准备变最
int shu1;
int shu2;
int he;
//2,录入数据
srand(time(0));     //获取当前时间(从1970年1月1日至今的秒数)作为初始化随机数种子,这样每次程序运行时都会产生不同的随机数.
shu1 = rand()%(99-10+1)+10;
shu2 = rand()%(99-10+1)+10;
cout<<"一道两位数加法测试题,请测试"<<endl;
cout<<shu1<<"+"<<shu2<<"=";
cin>>he;
// 3,计算shu1和shu2的和,
if(he==shu1+shu2)
{
    cout<<"恭喜您,答对了!"<<endl;
 }
 else
 {
    cout<<"你答错了"<<endl;
 }
 }