/* * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD * * SPDX-License-Identifier: MIT */ #pragma once #include #include #include #include #ifdef ESP_PLATFORM #include #endif namespace stackchan { class Random { public: static Random& getInstance() { static Random instance; return instance; } int getInt(int a, int b) { std::lock_guard lock(_mutex); if (a > b) { std::swap(a, b); } return std::uniform_int_distribution{a, b}(_rng); } float getFloat(float a, float b) { std::lock_guard lock(_mutex); if (a > b) { std::swap(a, b); } return std::uniform_real_distribution{a, b}(_rng); } double getDouble(double a, double b) { std::lock_guard lock(_mutex); if (a > b) { std::swap(a, b); } return std::uniform_real_distribution{a, b}(_rng); } template const T& choice(const std::vector& vec) { assert(!vec.empty()); std::lock_guard lock(_mutex); std::uniform_int_distribution dist(0, vec.size() - 1); return vec[dist(_rng)]; } private: std::mt19937 _rng; std::mutex _mutex; Random() { #ifdef ESP_PLATFORM _rng.seed(esp_random()); #else std::random_device rd; _rng.seed(rd()); #endif } }; } // namespace stackchan