C++基于隨機(jī)數(shù)實(shí)現(xiàn)福彩雙色球的方法示例
本文實(shí)例講述了C++基于隨機(jī)數(shù)實(shí)現(xiàn)福彩雙色球的方法。分享給大家供大家參考,具體如下:
這是前段時(shí)間寫(xiě)的福彩雙色球一個(gè)小應(yīng)用
本來(lái)可以一個(gè)文件搞定,反正也沒(méi)多大,就分開(kāi)了.
頭文件doubleColorBallR2.h
#ifndef _DoubleColorBallR2_h
#define _DoubleColorBallR2_h
#include <iostream>
#include <stdio.h>
#include <vector>
#include <list>
#include "windows.h"
#include <algorithm>
#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
using namespace std;
class DoubleColorBallR2
{
public:
DoubleColorBallR2(){
}
~DoubleColorBallR2(){
}
void getRedZone();
void getBlueZone();
private:
};
#endif
實(shí)現(xiàn)文件 doubleColorBallR2.cpp
#include "doubleColorBallR2.h"
void DoubleColorBallR2::getRedZone(){
vector<int> v_red; //選擇數(shù)的范圍 1-_RED_ZONE_
list<int> l_red; //裝入1-_RED_NUM_個(gè)數(shù)
for (int i=1; i <= _RED_ZONE_; ++i) {
v_red.push_back(i);
}
srand((unsigned)GetCurrentTime());
int j=_RED_ZONE_;
for (int i=1; i<=_RED_NUM_; ++i) {
int n=1+rand()%(j-1+1);
l_red.push_back(v_red[n]); //裝入
//刪除v_red已裝入的數(shù)
vector<int>::iterator iter=find(v_red.begin(), v_red.end(), v_red[n]);
v_red.erase(iter);
--j; //由于v_red已經(jīng)刪除了一位數(shù),所以隨機(jī)數(shù)取值范圍要減少一位
}
l_red.sort();
for (list<int>::iterator i=l_red.begin(); i!=l_red.end(); ++i) {
cout<<*i<<" ";
}
}
void DoubleColorBallR2::getBlueZone(){
vector<int> v_blue; //選擇數(shù)的范圍 1-_BLUE_ZONE_
list<int> l_blue; //裝入1-_BLUE_NUM_個(gè)數(shù)
for (int i=1; i <= _BLUE_ZONE_; ++i) {
v_blue.push_back(i);
}
srand((unsigned)GetCurrentTime());
int j=_BLUE_ZONE_;
for (int i=1; i<=_BLUE_NUM_; ++i) {
int n=1+rand()%(j-1+1);
l_blue.push_back(v_blue[n]); //裝入
//刪除v_red已裝入的數(shù)
vector<int>::iterator iter=find(v_blue.begin(), v_blue.end(), v_blue[n]);
v_blue.erase(iter);
--j; //由于v_red已經(jīng)刪除了一位數(shù),所以隨機(jī)數(shù)取值范圍要減少一位
}
l_blue.sort();
for (list<int>::iterator i=l_blue.begin(); i!=l_blue.end(); ++i) {
cout<<*i<<" ";
}
}
主程序 doubleColorBall.cpp
#include <iostream>
#include <list>
#include "windows.h"
#include <stdio.h>
#include "doubleColorBallR2.h"
#define RED_ZONE 33 //紅區(qū)
#define BLUE_ZONE 16 //藍(lán)區(qū)
#define RED_NUM 6 //紅區(qū)位數(shù)
#define BLUE_NUM 1 //藍(lán)區(qū)位數(shù)
using namespace std;
int main (int argc, char *argv[])
{
DoubleColorBallR2 dcb;
dcb.getRedZone();
dcb.getBlueZone();
getchar();
return(0);
}
PS:這里再為大家提供兩款功能類似的在線工具供大家參考:
在線隨機(jī)數(shù)字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高強(qiáng)度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword
希望本文所述對(duì)大家C++程序設(shè)計(jì)有所幫助。
- C++ 隨機(jī)數(shù)字以及隨機(jī)數(shù)字加字母生成的案例
- C++生成隨機(jī)數(shù)的實(shí)現(xiàn)代碼
- C++編程產(chǎn)生指定范圍內(nèi)的隨機(jī)數(shù)
- C++實(shí)現(xiàn)產(chǎn)生隨機(jī)數(shù)和相應(yīng)的猜拳小游戲?qū)嵗a
- C++常見(jiàn)獲取隨機(jī)數(shù)的方法小結(jié)
- C++ 隨機(jī)數(shù)與隨機(jī)種子數(shù)的實(shí)例
- 簡(jiǎn)單談?wù)勱P(guān)于C++中大隨機(jī)數(shù)的問(wèn)題
- C語(yǔ)言/C++如何生成隨機(jī)數(shù)
- C++編寫(xiě)生成不重復(fù)的隨機(jī)數(shù)代碼
- c++ 隨機(jī)數(shù)問(wèn)題的相關(guān)研究
相關(guān)文章
C語(yǔ)言深度解剖篇之關(guān)鍵字以及補(bǔ)充內(nèi)容
C語(yǔ)言的關(guān)鍵字共有32個(gè),根據(jù)關(guān)鍵字的作用,可分其為數(shù)據(jù)類型關(guān)鍵字、控制語(yǔ)句關(guān)鍵字、存儲(chǔ)類型關(guān)鍵字和其它關(guān)鍵字四類,這篇文章主要給大家介紹了關(guān)于C語(yǔ)言深度解剖篇之關(guān)鍵字以及補(bǔ)充內(nèi)容的相關(guān)資料,需要的朋友可以參考下2022-06-06
C語(yǔ)言結(jié)構(gòu)體內(nèi)存的對(duì)齊知識(shí)詳解
這篇文章主要介紹了C語(yǔ)言結(jié)構(gòu)體內(nèi)存的對(duì)齊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Opencv實(shí)現(xiàn)用于圖像分割分水嶺算法
這篇文章主要為大家詳細(xì)介紹了Opencv實(shí)現(xiàn)用于圖像分割分水嶺算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
用C語(yǔ)言實(shí)現(xiàn)鏈?zhǔn)綏=榻B
大家好,本篇文章主要講的是用C語(yǔ)言實(shí)現(xiàn)鏈?zhǔn)綏=榻B,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12

