C++随机函数玩法大揭秘,超酷随机函数、抽奖系统(初学者消遣专用)(初学稍进阶)
【C++随机函数趣味应用】本文介绍了C++中随机函数的多种趣味玩法,包括:1️⃣ 运势测试(0-100随机数);2️⃣ 抽奖系统(多级奖励机制);3️⃣ 积分挑战游戏(三级关卡);4️⃣ AI石头剪刀布。重点讲解了srand(time(0))初始化随机种子和rand()%n的用法,并提供了完整的代码示例。文章采用轻松幽默的风格,适合初学者学习随机函数的实际应用。
装13课!!!
我相信大家在学习或者用C++时或多或少会有些乏累、疲倦。但好在你们有。。。(不对,是本来就有)C++!!!
提到C++,我要开心死了呢。。。那么正式开始这节我们的随机函数,虽然说起来非常高级,但要写的话,只要调用即可,用起来很🐮🍺
让我们来看一下吧!
#include <iostream>
#include <ctime>
#include <cstdlib>
👆这是需要的头文件,但个人还是比较推荐万能头(作为善良的人,再给你们写一遍吧)⬇️
#include <bits/stdc++.h>
框架呢,可以看我的这节👉C++基本框架,基础(初学者推荐)
好吧,我听见了你们的心声,我就再写一遍吧(我这么善良,就给我点个赞吧,求求了qwq)
#include <bits/stdc++.h>
using namespace std;
int main()
{
return 0;
}
具体该怎么写呢,我给你们放出来,你们可以玩玩,尝试理解一下
#include <iostream>//引入标准输入输出库,用于程序中的输入输出操作
#include <ctime>//引入时间库,提供时间相关功能(如time(0)用于获取当前时间戳)
#include <cstdlib>//引入标准库,包含随机数生成函数(如srand()和rand())
//上述三行代码可以替换成一行#include <bits/stdc++.h>
using namespace std;
int main()
{
srand(time(0)); //用当前时间戳初始化随机数种子,确保每次运行程序时生成的随机数不同
cout << "爱编程的tony 此刻运势:" << endl;//这行可以去掉哈
cout << rand()%101<< endl;
//rand()生成伪随机整数
//%101将随机数范围限制在0-100
return 0;
}
如果你已经会上面代码,就大差不差可以完成这个代码⬇️
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0)); // 设置随机数种子
int tony = rand() % 101;
int me = rand() % 101;
cout << "tony的今日运势:" << endl;
cout <<tony<< endl;
cout << "你的今日运势:" << endl;
cout << me << endl;
if (me > tony)
{
cout << "你更幸运!" << endl;
}
else
{
cout << "tony更幸运!" << endl;
}
return 0;
}
如果这还不够,看下一个!这就是我说的抽奖系统!!!
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int a, score;
cout << "输入任意数字第一次开箱:" << endl;
cin >> a;
score = rand()%41 + 60;
cout << "本次获得积分:" << score << endl << endl;
if (score >= 95)
{
cout << "【欧皇装备】" << endl;
}
else
{
cout << "啥都没中!输入任意数字第二次开箱:" << endl;
cin >> a;
score += rand()%41 + 60;
cout << "本次开箱后共有积分:" << score << endl << endl;
if (score >= 170)
{
cout << "【稀有皮肤】" << endl;
}
else
{
cout << "啥都没中!输入任意数字第三次开箱:" << endl;
cin >> a;
/*把第三次开箱的分数累加到score中*/
score += rand()%41 + 60;
cout << "本次开箱后共有积分:" << score << endl << endl;
if (score >= 240)
{
cout << "【灵兽】" << endl;
}
else
{
cout << "【谢谢光临!】" << endl;
}
}
}
return 0;
}
你不会以为我没有其他好玩的吧,如果你这么想就错了,我还有!!
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int a, tot = 150, score = tot;
if (score >= 100)
{
score -= 100; //开箱消耗100积分
cout << "【你当然可以走到这里!】" << endl ;
cout << "输入任意数字开始吧:" << endl;
cin >> a;
int res = rand()%150 + 1; //随机产生1~150的积分
score += res; //将这一轮产生的积分累加到score中
cout << "^_^本关结束,目前积分为:" << score << endl;
if (score >= 100)
{
score -= 100;
cout << "【看来你赚了不少积分!】" << endl << endl;
cout << "输入任意数字继续:" << endl;
cin >> a;
res = rand()%150 + 1;
score += res;
cout << "^_^本关结束,目前积分为:" << score << endl;
if (score >= 100)
{
score -= 100;
cout << "【竟然能走到最后一关!】" << endl << endl;
cout << "来吧,输入任意数字一决胜负:" << endl;
cin >> a;
res = rand()%150 + 1;
score += res;
cout << "^_^本关结束,目前积分为:" << score << endl << endl;
}
else
{
cout << "【哦吼,已经不错啦!】" << endl;
}
}
else
{
cout << "【有缘再见啦!】" << endl;
}
}
else
{
cout << "【一次都没有?出bug啦!】" << endl;
}
cout << "本次活动结束,目前积分为:" << score << endl;
if (score > 150) //判断最终积分是否多于开始积分
{
cout << "【You're a lucky guy!】";
}
else
{
cout << "【What happened?】";
}
return 0;
}
我这波福利怎么样吧!下面是与AI的石头剪刀布
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
// 玩家出招
cout << "请输入你的选择:"<< endl;
cout << "1、石头" << endl;
cout << "2、剪刀" << endl;
cout << "3、布" << endl;
int a;
cin >> a;
cout << "你的选择是:" << a << endl;
if(a == 1)
{
cout << "你出的石头" << endl;
}
if(a == 2)
{
cout << "你出的剪刀" << endl;
}
if(a == 3)
{
cout << "你出的布" << endl;
}
// AI出招
srand(time(0));
int b = rand()%3 + 1;
if(b == 1)
{
cout << "AI出的石头" << endl;
}
if(b == 2)
{
cout << "AI出的剪刀" << endl;
}
if(b == 3)
{
cout << "AI出的布" << endl;
}
// 判断胜负
// 使用if嵌套来完成判断
if(a == b)
{
cout << "平手" <<endl;
}
else if(a - b == -1)
{
cout << "你赢了" ;
}
else if(a - b == 2)
{
cout << "你赢了" ;
}
else
{
cout << "你输了" ;
}
return 0;
}
其实我还有一堆好玩的呢!下期再说,这一节都3000多字了,你们应该能玩一时半会儿的,反正看的人并不多(跟大家说一下,我可能上线不会很多了,毕竟国庆结束了,大家想催的话可以私信我,我尽量回关,抽时间去看,其实这篇是我在10月8日就已经完稿了,但因为学业原因可能会晚发,请大家多多见谅,还有就是这个CSDN一天只能提交3篇稿,气死我了😤,所以当天发布不过去,尽量早发嘿嘿,以后更新频率不会太快了哈)
好的要结束了,我要说结束语啦
谢谢观看,如果可以的话关注我吧♥️,不知道B站你们刷不刷,如果刷的话关注我一下呗,平时玩玩游戏啥的(不关注也没事,粉丝反正不多,我也不会顺着网线去掏你家的,要加的话,这是我B站名字👉 疯狂的小腾
更多推荐
所有评论(0)