Extra2D/include/utils/random.h

78 lines
2.0 KiB
C
Raw Normal View History

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