鱼C论坛

 找回密码
 立即注册
查看: 1265|回复: 4

[已解决]rand

[复制链接]
发表于 2020-10-11 11:55:44 | 显示全部楼层    本楼为最佳答案   
  1. #include<iostream>
  2. #include<string>
  3. #include<math.h>
  4. #include<iomanip>
  5. #include<stdio.h>
  6. #include<ctime>
  7. #include<cstdlib>
  8. using namespace std;
  9. int casual()
  10.         {
  11.         
  12.         int a = rand() % 6 + 1;
  13.         //srand(2);
  14.         int b = rand() % 6 + 1;
  15.         
  16.         int sum = a + b;
  17.         cout<<a<<"+"<<b<<"="<<sum<<endl;
  18.         return sum;
  19.         
  20.         }
  21. main()
  22. {
  23.                 srand(time(0)); // 把种子放在主函数里,确保只调用一次
  24.         int any = casual();
  25.         cout<<any<<endl;
  26.         int any1 = casual();
  27.         cout<<any1<<endl;
  28.         int any2 = casual();
  29.         cout<<any2<<endl;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-5 04:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表