21 #include "hx3d/math/random.hpp" 22 #include "hx3d/utils/algorithm.hpp" 23 #include "hx3d/utils/log.hpp" 30 WeightedRandom::WeightedRandom() {}
33 if (_weights.find(i) == _weights.end()) {
38 Log.
Error(
"WeightedRandom: Already defined '%d'", i);
49 int WeightedRandom::get_number(
int gen) {
53 for (
int i = 0; i < _weights[n]; ++i) {
64 int WeightedRandom::total_count() {
65 return algo::reduce(_vec, 0, [
this](
int res,
int val){
return res + _weights[val]; });
71 return (
float)rand() / (RAND_MAX + 1.f);
75 return rand() % (max-min + 1) + min;
float randfloat()
Generate a random float between 0.0 and 1.0.
Type reduce(Container &container, Type init)
Apply a simple reduce.
int random()
Choose a random number and execute the callback.
void Error(const std::string fmt,...)
Write an error message.
static hx3d::LogImpl Log
Current log implementation.
void define(int i, int weight, std::function< void()> f)
Define a value with a weight and a callback.
int random(int min, int max)
Generate a random integer between min and max.
bool flip_coin()
Generate a random boolean.