随机函数的应用:生成双色球号码
一、前导
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 result
return 0;
}
- 计算机在执行程序时,速度非常快,往往1000轮循环仅需要不到1秒,从而导致根据当前时间生成的随机数都是一样的,因此需要加入等待时间
#include <ctime>
#define FirstPart 34
#define One 1
seed=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 6
void 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;
else
Bless_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 signal
cout << endl;
cout << "Please enter any key to end...";
_getch(); //fix the bug : user can't see the result
return 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
*/