78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <random>
|
|
#include <types/base/types.h>
|
|
|
|
namespace extra2d {
|
|
|
|
// ============================================================================
|
|
// Random 类 - 随机数生成器
|
|
// 非单例设计,可以创建多个独立的随机数生成器实例
|
|
// ============================================================================
|
|
class Random {
|
|
public:
|
|
/// 默认构造函数(使用随机种子)
|
|
Random();
|
|
|
|
/// 使用指定种子构造
|
|
explicit Random(uint32 seed);
|
|
|
|
/// 设置随机种子
|
|
void setSeed(uint32 seed);
|
|
|
|
/// 使用当前时间作为种子
|
|
void randomize();
|
|
|
|
/// 获取 [0, 1) 范围内的随机浮点数
|
|
float getFloat();
|
|
|
|
/// 获取 [min, max] 范围内的随机浮点数
|
|
float getFloat(float min, float max);
|
|
|
|
/// 获取 [0, max] 范围内的随机整数
|
|
int getInt(int max);
|
|
|
|
/// 获取 [min, max] 范围内的随机整数
|
|
int getInt(int min, int max);
|
|
|
|
/// 获取随机布尔值
|
|
bool getBool();
|
|
|
|
/// 获取随机布尔值(带概率)
|
|
bool getBool(float probability);
|
|
|
|
/// 获取指定范围内的随机角度(弧度)
|
|
float getAngle();
|
|
|
|
/// 获取 [-1, 1] 范围内的随机数(用于方向)
|
|
float getSigned();
|
|
|
|
private:
|
|
std::mt19937 generator_;
|
|
std::uniform_real_distribution<float> floatDist_;
|
|
};
|
|
|
|
// ============================================================================
|
|
// 便捷函数 - 使用全局默认随机数生成器
|
|
// ============================================================================
|
|
|
|
/// 获取 [0, 1) 范围内的随机浮点数
|
|
float randomFloat();
|
|
|
|
/// 获取 [min, max] 范围内的随机浮点数
|
|
float randomFloat(float min, float max);
|
|
|
|
/// 获取 [0, max] 范围内的随机整数
|
|
int randomInt(int max);
|
|
|
|
/// 获取 [min, max] 范围内的随机整数
|
|
int randomInt(int min, int max);
|
|
|
|
/// 获取随机布尔值
|
|
bool randomBool();
|
|
|
|
/// 获取随机布尔值(带概率)
|
|
bool randomBool(float probability);
|
|
|
|
} // namespace extra2d
|