鱼C论坛

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

[已解决]rand

[复制链接]
发表于 2020-10-11 10:56:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  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.         srand(time(0));
  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.         int any = casual();
  24.         cout<<any<<endl;
  25.         any = casual();
  26.         cout<<any<<endl;
  27.         any = casual();
  28.         cout<<any<<endl;
  29. }
复制代码

为什么生成不了随机数
最佳答案
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. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-11 11:00:45 | 显示全部楼层
C++吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-11 11:18:56 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 12:19:22 | 显示全部楼层
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. int casual()
  6. {
  7.         int a,b,sum=0;
  8.         a = rand() % 6 + 1;
  9.         srand(2);
  10.         b = rand() % 6 + 1;
  11.         sum = a + b;
  12.        
  13.         return sum;  
  14. }
  15. int main()
  16. {
  17.         int any = casual();
  18.         srand(time(0));
  19.         cout<<any<<endl;
  20.         any = casual();
  21.         cout<<any<<endl;
  22.         any = casual();
  23.         cout<<any<<endl;
  24.        
  25.         return 0;
  26. }
复制代码


我试了几次,感觉每次出现相同数的几率很大
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 11:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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