mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
feat(common): add utils::random
This commit is contained in:
26
src/common/utils.cpp
Normal file
26
src/common/utils.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* @file utils.cpp
|
||||||
|
*
|
||||||
|
* @author Yuzhe Guo
|
||||||
|
* @date 2025.11.15
|
||||||
|
*/
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
namespace UNO::COMMON {
|
||||||
|
Random::Random() : rd_(), gen_(rd_()) {}
|
||||||
|
std::mt19937 &Random::getGenerator() {
|
||||||
|
return gen_;
|
||||||
|
}
|
||||||
|
|
||||||
|
Utils *Utils::getInstance() {
|
||||||
|
if (Utils::instance_ == nullptr) {
|
||||||
|
Utils::instance_ = new Utils();
|
||||||
|
}
|
||||||
|
return Utils::instance_;
|
||||||
|
}
|
||||||
|
Random &Utils::getRandom() {
|
||||||
|
return this->random_;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // UNO
|
||||||
50
src/common/utils.h
Normal file
50
src/common/utils.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
* @file utils.h
|
||||||
|
* 工具
|
||||||
|
*
|
||||||
|
* @author Yuzhe Guo
|
||||||
|
* @date 2025.11.15
|
||||||
|
*/
|
||||||
|
#ifndef UNO_GAME_UTILS_H
|
||||||
|
#define UNO_GAME_UTILS_H
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
namespace UNO::COMMON {
|
||||||
|
/**
|
||||||
|
* 随机数生成器
|
||||||
|
*/
|
||||||
|
class Random {
|
||||||
|
private:
|
||||||
|
std::random_device rd_;
|
||||||
|
std::mt19937 gen_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Random();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return 一个 std::mt19937
|
||||||
|
*/
|
||||||
|
std::mt19937 &getGenerator();
|
||||||
|
};
|
||||||
|
|
||||||
|
class Utils {
|
||||||
|
private:
|
||||||
|
Utils() = default;
|
||||||
|
Random random_;
|
||||||
|
static Utils* instance_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @return 随机数生成器
|
||||||
|
*/
|
||||||
|
Random &getRandom();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return 工具实例
|
||||||
|
*/
|
||||||
|
static Utils *getInstance();
|
||||||
|
|
||||||
|
};
|
||||||
|
} // UNO
|
||||||
|
|
||||||
|
#endif //UNO_GAME_UTILS_H
|
||||||
Reference in New Issue
Block a user