模拟自选
//模拟自选
/*大乐透投注区分为前区号码和后区号码,前区号码范围为01~35,后区号码范围为01~12。
大乐透每期从35个前区号码中开出5个号码,
从12个后区号码中开出2个号码作为中奖号码,大乐透玩法即是竞猜开奖号码的5个前区号码和2个后区号码,顺序不限。
*/#include <iostream>//c ++输入输出
#include <stdlib.h>//含srand()选种子函数,rand随机函数
#include <ctime>//time()函数 获取当前时间
#include <unistd.h>//延时函数
#define Max1 35 //5个前区范围 1 ~ 35
#define Max2 12 //2个后区范围 1 ~ 12
using namespace std;//判断是否重号(前区不能重号,后区不能重号),前区可能与后区有同号
int judge(int a[],int x)
{if(a[0] == 0){return 0;}int i = 0;for(i = 0;a[i] != 0;i ++){if(a[i] == x)//判断是否重号{return 1;//是 }}return 0;
}//有重号 返回 1,没有重号返回0 int main()
{int count = 1,x = 0,rand1,rand2;rand1 = rand2 = 1;while(1){int front[5] = {0},back[2] = {0};//每次初始化 前区,后区数组 cout << "第" << count <<"次生成号码:";//前区 srand((unsigned)time(NULL));//以当前时间为种子(可以不用unsigned)for(int i = 0;i < 5;)//5 个前区 {rand1 = rand() % Max1 + 1;if(judge(front,rand1) == 1)//有重号 {continue;//舍去该号码重新选 }front[i] = rand1;cout << rand1 << " ";//rand() 内部实现是用线性同余法做的sleep(1);i ++;//选下一个号码 }//后区 srand((unsigned)time(NULL));//以当前时间为种子(可以不用unsigned)for(int i = 0;i < 2;)//2个后区{rand2 = rand() % Max2 + 1;if(judge(back,rand2) == 1)//有重号 {continue;//舍去该号码重新选 }back[i] = rand2;cout << rand2 << " ";sleep(1);i ++;//选下一个号码 }cout << endl;count ++;cout << "退出输入2,继续请输入1: ";cin >> x;if(x == 2){break;}else;cout << endl;}return 0;
}
/*
有时程序需要一个特定范围内的随机数。要将随机数的范围限制在 1 和某个最大值 max 之间的整数,可以使用以下公式:
number = rand() % Max + 1;
*/