随机数库random
中随机数引擎random-number engins
和随机数分布类random-number distribution
。
param_gen.h
#pragma once
#include <random>
class Random {
public:
Random(unsigned min, unsigned max)
: min(min), max(max), u(min, max) {}
unsigned get_random_int() {
return u(e);
}
private:
unsigned min = 0;
unsigned max = 9;
std::uniform_int_distribution<unsigned> u;
std::default_random_engine e;
};
utility.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
bool validate(const std::vector<unsigned> &v) {
bool flag = true;
for (auto itr = v.cbegin(); itr != v.cend(); ++itr) {
if (*itr != 6) {
flag = false;
}
}
return flag;
}
void go_through(const std::vector<unsigned> &v) {
for (const auto &i:v) {
std::cout << i << " ";
}
std::cout << std::endl;
}
main.cpp
#include <iostream>
#include <string>
#include <vector>
#include "param_gen.h"
#include "utility.h"
using namespace std;
int main() {
cout << "Welcome to tiger machine" << endl;
Random rom(0, 9);
char cmd;
cout << "Get start? Y/N" << endl;
while (cin >> cmd && cmd =='Y') {
vector<unsigned> v;
for (size_t i = 0; i < 3; ++i) {
v.push_back(rom.get_random_int());
}
go_through(v);
if (validate(v)) {
cout << "Congratulation! You get 666" << endl;
} else {
cout << "Ops, try it again? Y/N" << endl;
}
}
}