小游戏之天选之人:通过幸运数字生成双色球号码(随机函数的应用)
随机函数的应用:生成双色球号码
- 一、前导
- 1. 需要掌握的知识
- 2. 程序简介
- 二、思路分析
- 三、具体实现
- 1. 弯路和bug
- 2. 代码框架(重点)
- 2.1 采用的数据结构
- 2.2 程序主体框架
- 2.3 各分支函数
- 3. 完整编码
一、前导
1. 需要掌握的知识
- 流程知识:了解福彩双色球的基本规则,红色从1-33中选取6个不重复号码,蓝色从1-16中选取一个号码
- 随机函数: srand(), rand() ,切记:rand()函数是个伪随机,如果不执行 srand()生成新的种子,rand()函数每次生成的随机数都相同
2. 程序简介
- 交给机器去选择,不如交给自己的代码去选择,最起码可以和自己的幸运数字产生关联 O(∩_∩)O
- 郑重声明:本程序仅仅是个游戏,别当真,程序仅仅是对随机函数的编码练习和应用
二、思路分析
总体:数据结构选择数组,数组的下标对应球的编码,数组元素的大小对应该球抓取的次数;抓取(循环)结束后,红球数组中挑选出排名前6的元素下标、蓝球数组中挑选最大的元素下标
- 根据用户输入的数字生成循环次数 并 控制每次循环的时间(控制了循环时间,也就影响了每次生成的数字)
- 在循环中,每次循环通过当前时间生成种子并最终生成数字(需要通过求余控制生成的数字范围);将生成的数字记录到数组中,数字对应数组的下标,那么数组值最大的元素 所对应的下标就是抓取次数最多的球
- 循环执行完毕,在红色数组中找到最大的六个元素的数组下标,蓝色数组中找一个。随后打印结果即可
三、具体实现
1. 弯路和bug
- 程序执行后会闪退,导致无法看到运行结果,需要通过_getch()避免程序自动退出。_getch()对应库文件conio.h
#include<conio.h>
int main()
{cout<<"Please enter any key to end...";_getch(); fix the bug : user can't see the resultreturn 0;
}
- 计算机在执行程序时,速度非常快,往往1000轮循环仅需要不到1秒,从而导致根据当前时间生成的随机数都是一样的,因此需要加入等待时间
#include <ctime>
#define FirstPart 34
#define One 1seed=time(0); //通过系统time()获取当前时间
Bless(Bless_time*1000); //通过Bless()子函数控制等待,Bless_time就是用户输入的幸运数字
srand(seed);
position=rand()%(FirstPart-One)+One;void Bless(int time)
{clock_t now=clock();while(clock()-now<time);
}
2. 代码框架(重点)
2.1 采用的数据结构
使用数组记录每次抽选出的数字,数组的索引对应球的编号
#define FirstPart 34
#define LastPart 17
int red[FirstPart]={0};
int blue[LastPart]={0};
2.2 程序主体框架
程序伪码描述
int main()
{ 1.根据用户录入的幸运数字生成循环次数(抓球的次数)2.while(循环次数){根据当前时间生成编号,并记录到对应的数组元素中;比如:red阶段生成的编码是3,red[3]+1;}3.根据Findmax()找到red[]数组中值最大的六个元素,并返回数组下标;4.排列这些下标并打印(blue同理)备注:通过srand()生成种子, 通过rand() 生成随机数
}
2.3 各分支函数
- FindMax() 找到数组中最大元素的下标
int FindMax(int a[],int size)
{int max=0,position=0; for(int i=1;i<size;i++)if(max<a[i]){max=a[i];position=i;} return position;
}
- Bless() 控制每次祈祷(循环)时间
void Bless(int time)
{clock_t now=clock();while(clock()-now<time);
}
3. 完整编码
//unfixed bug: if your input number exceed 10000000,the programe will crash
#include<conio.h> //fix the bug : user can't see the result
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;#define FirstPart 34
#define LastPart 17
#define One 1
#define redNumber 6void Bless(int time);
int FindMax(int a[], int size);int main()
{int red[FirstPart] = { 0 };int blue[LastPart] = { 0 };int luck, seed, loop_number, position;int redResult[redNumber] = { 0 };int blueResult = 0; int Bless_time = 0;int tmp;cout << "Your Lucky Number: ";cin >> luck;/*if (luck >= 10) //fix the bug:Bless time is too long!Bless_time = luck % 10 + One;elseBless_time = luck; */Bless_time = luck;srand(luck);loop_number = rand();while (loop_number--) //red part{cout << "Bless the Red... Remaining time " << loop_number * Bless_time << " Second" << endl;seed = time(0);Bless(Bless_time * 1000);srand(seed);position = rand() % (FirstPart - One) + One;red[position]++;//loop_number--;}for (int j = 0; j < redNumber; j++){tmp = FindMax(red, FirstPart);redResult[j] = tmp;red[tmp] = 0;}sort(redResult, redResult + redNumber);srand(luck);loop_number = rand();while (loop_number) //blue part{cout << "Bless the Blue... Remaining time " << loop_number * Bless_time << " Second" << endl;seed = time(0);Bless(Bless_time * 1000);srand(seed);position = rand() % (LastPart - One) + One;blue[position]++;loop_number--;}blueResult = FindMax(blue, LastPart);cout << endl;cout << "The Chosen One : ";for (int i = 0; i < redNumber; i++) cout << ' ' << redResult[i];cout << ' - ' << blueResult; //fix the bug : add the seprate signalcout << endl;cout << "Please enter any key to end...";_getch(); //fix the bug : user can't see the resultreturn 0;
}void Bless(int time)
{clock_t now = clock();while (clock() - now < time);
}int FindMax(int a[], int size)
{int max = 0, position = 0;for (int i = 1; i < size; i++)if (max < a[i]){max = a[i];position = i;}return position;
}/*
1.array : record the random number
2. find the max
3. sort
*/
小游戏之天选之人:通过幸运数字生成双色球号码(随机函数的应用)相关推荐
- Python 实现随机生成双色球号码
输入生成的双色球组数,随机批量生成双色球号码 幸运数字为蓝色球 import random print("*****福彩双色球*****") print("======= ...
- python 双色球 输出到txt_python3 - 写一个生成双色球号码的一个程序,生成的号码写到文件里面...
写一个生成双色球号码的一个程序,生成的号码写到文件里面 # 中奖号码由6个红色球号码和1个蓝色球号码组成 # 篮球范围:01-16 # 红球范围:01-33 def swq(num): random. ...
- shell脚本生成双色球号码
shell脚本生成双色球号码 双色球号码生成规则 shell脚本实现 shell运行结果展示 双色球号码生成规则 1.第七条"双色球"每注投注号码由6个红色球号码和1个蓝色球号码组 ...
- 用数组实现随机生成双色球号码
每日作业 实现随机生成双色球号码 红球33个球(01-33)取6个 蓝球16个球(01-16)取1个 提示: 蓝球池{"01"....."16"}: 红球池{& ...
- 编程练习:福利彩票中心,实现随机生成双色球号码,实现下注号码对比。 基础知识:1.数组 2.循环逻辑
基础知识:1.数组 2.循环逻辑 实现随机生成双色球号码: [ 04 12 30 08 15 26 ] [ 11 ] 思路: 蓝球池 { "01","02",& ...
- 微信小游戏开发实战教程13-随机生成形状功能的实现
微信小游戏开发实战系列的第13篇. 本节内容包括:通过使用列表来实现可以设置权重的随机功能,以及游戏中的随机生成形状功能的具体实现. 如果你没有任何的游戏开发经验,欢迎阅读我的"人人都能做游 ...
- 一个小游戏的代码(猜数字)
#include <time.h> #include <stdlib.h> #include <stdio.h> //小游戏void mu() {printf(&q ...
- C语言小游戏第二弹~1-100猜数字(无聊时候摸鱼必备)
hello uu们,我们本期的小游戏是在1-100里面猜数字,无聊摸鱼必备小游戏哦~~ 闲话少谈,整活走起 首先我们可以简单的把这个程序主题分成两大块 1.游戏界面 2.猜数字过程主体 下面开始分步详 ...
- 关于4和7的幸运数字生成方法
所谓的4和7幸运数字指:4.7.44.47.74.77.444-- 第一个幸运数字为4,第七个幸运数字为444 以下采用了两种方法求第n位的幸运数字: 方法一:二进制思想 function lucky ...
最新文章
- 强化学习在机器人装配工艺中的应用
- laravel 5.5 的相关字符串辅助函数
- Windows bat with adb
- 冒泡排序的原理,思路,以及算法分析(Java实现)
- Apache Digester示例–轻松配置
- 远程连接:hive 报错:Permission denied: user=anonymous, access=EXECUTE
- ES6新特性_ES6箭头函数以及声明特点---JavaScript_ECMAScript_ES6-ES11新特性工作笔记009
- mysql二进制安装shell脚本,一分钟就搞好linux上的mysql
- Hack with Lenna | PIX与诗歌、自动驾驶和图像识别
- 怎么把多个pdf文件合并成一个?
- c++ - 虚函数表
- React开发者工具 React Developer Tools 的下载
- 一文极速读懂 KEGG 数据库
- 在百度地图中获取当前点击 位置的 经纬度 和 地址信息
- pp什么意思_在网络用语中PP是什么意思?
- 云计算学习路线教程大纲课件:部署论坛系统Discuz
- 全系T*镀膜认证镜头 vivo X70系列夜拍体验再升级
- Python 游戏开发: 外星人入侵
- NSX-T 恢复DFW策略
- linux安全(1)