C++基于随机数实现福彩双色球的方法示例
本文实例讲述了C++基于随机数实现福彩双色球的方法。分享给大家供大家参考,具体如下:
这是前段时间写的福彩双色球一个小应用
本来可以一个文件搞定,反正也没多大,就分开了.
头文件doubleColorBallR2.h
#ifndef_DoubleColorBallR2_h #define_DoubleColorBallR2_h #include#include #include #include #include"windows.h" #include
#ifndef_RED_ZONE_ #define_RED_ZONE_33 #endif #ifndef_BLUE_ZONE_ #define_BLUE_ZONE_16 #endif #ifndef_RED_NUM_ #define_RED_NUM_6 #endif #ifndef_BLUE_NUM_ #define_BLUE_NUM_1 #endif usingnamespacestd; classDoubleColorBallR2 { public: DoubleColorBallR2(){ } ~DoubleColorBallR2(){ } voidgetRedZone(); voidgetBlueZone(); private: }; #endif
实现文件doubleColorBallR2.cpp
#include"doubleColorBallR2.h"
voidDoubleColorBallR2::getRedZone(){
vectorv_red;//选择数的范围1-_RED_ZONE_
listl_red;//装入1-_RED_NUM_个数
for(inti=1;i<=_RED_ZONE_;++i){
v_red.push_back(i);
}
srand((unsigned)GetCurrentTime());
intj=_RED_ZONE_;
for(inti=1;i<=_RED_NUM_;++i){
intn=1+rand()%(j-1+1);
l_red.push_back(v_red[n]);//装入
//删除v_red已装入的数
vector::iteratoriter=find(v_red.begin(),v_red.end(),v_red[n]);
v_red.erase(iter);
--j;//由于v_red已经删除了一位数,所以随机数取值范围要减少一位
}
l_red.sort();
for(list::iteratori=l_red.begin();i!=l_red.end();++i){
cout<<*i<<"";
}
}
voidDoubleColorBallR2::getBlueZone(){
vectorv_blue;//选择数的范围1-_BLUE_ZONE_
listl_blue;//装入1-_BLUE_NUM_个数
for(inti=1;i<=_BLUE_ZONE_;++i){
v_blue.push_back(i);
}
srand((unsigned)GetCurrentTime());
intj=_BLUE_ZONE_;
for(inti=1;i<=_BLUE_NUM_;++i){
intn=1+rand()%(j-1+1);
l_blue.push_back(v_blue[n]);//装入
//删除v_red已装入的数
vector::iteratoriter=find(v_blue.begin(),v_blue.end(),v_blue[n]);
v_blue.erase(iter);
--j;//由于v_red已经删除了一位数,所以随机数取值范围要减少一位
}
l_blue.sort();
for(list::iteratori=l_blue.begin();i!=l_blue.end();++i){
cout<<*i<<"";
}
}
主程序doubleColorBall.cpp
#include#include #include"windows.h" #include
#include"doubleColorBallR2.h" #defineRED_ZONE33//红区 #defineBLUE_ZONE16//蓝区 #defineRED_NUM6//红区位数 #defineBLUE_NUM1//蓝区位数 usingnamespacestd; intmain(intargc,char*argv[]) { DoubleColorBallR2dcb; dcb.getRedZone(); dcb.getBlueZone(); getchar(); return(0); }
PS:这里再为大家提供两款功能类似的在线工具供大家参考:
在线随机数字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高强度密码生成器:
http://tools.jb51.net/password/CreateStrongPassword
希望本文所述对大家C++程序设计有所帮助。