C++ random number based on the implementation of lucky lottery the double chromosphere method example

  • 2020-05-26 09:41:23
  • OfStack

The example of this paper describes the method of C++ to realize lucky lottery the double chromosphere based on random Numbers. I will share it with you for your reference as follows:

This is a short time ago written blessing lottery the double chromosphere 1 small application

Originally can 1 file is done, anyway also not big, split.

The header file 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

Implementation file doubleColorBallR2.cpp


#include "doubleColorBallR2.h"
void DoubleColorBallR2::getRedZone(){
  vector<int> v_red; // The range of options  1-_RED_ZONE_
  list<int> l_red;  // loading 1-_RED_NUM_ The number of 
  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]); // loading 
    // delete v_red The number loaded 
    vector<int>::iterator iter=find(v_red.begin(), v_red.end(), v_red[n]);
    v_red.erase(iter);
    --j; // Due to the v_red It's been deleted 1 digits , So the range of random Numbers has to be reduced 1 position 
  }
  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; // The range of options  1-_BLUE_ZONE_
  list<int> l_blue;  // loading 1-_BLUE_NUM_ The number of 
  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]); // loading 
    // delete v_red The number loaded 
    vector<int>::iterator iter=find(v_blue.begin(), v_blue.end(), v_blue[n]);
    v_blue.erase(iter);
    --j; // Due to the v_red It's been deleted 1 digits , So the range of random Numbers has to be reduced 1 position 
  }
  l_blue.sort();
  for (list<int>::iterator i=l_blue.begin(); i!=l_blue.end(); ++i) {
    cout<<*i<<" ";
  }
}

The main program doubleColorBall cpp


#include <iostream>
#include <list>
#include "windows.h"
#include <stdio.h>
#include "doubleColorBallR2.h"
#define RED_ZONE 33 // Red zone 
#define BLUE_ZONE 16 // The blue area 
#define RED_NUM 6  // Red zone digits 
#define BLUE_NUM 1 // The blue location number 
using namespace std;
int main (int argc, char *argv[])
{
  DoubleColorBallR2 dcb;
  dcb.getRedZone();
  dcb.getBlueZone();
  getchar();
  return(0);
}

PS: here are two other online tools with similar functions for your reference:

Online random number/string generation tool:
http://tools.ofstack.com/aideddesign/suijishu

High strength password generator:
http://tools.ofstack.com/password/CreateStrongPassword

I hope this article is helpful to you C++ programming.


Related articles: