hx3d  1
2D/3D Simple Game Framework
Public Member Functions | List of all members
hx3d::math::WeightedRandom Class Reference

Weighted random generator with callbacks. More...

#include <random.hpp>

Public Member Functions

void define (int i, int weight, std::function< void()> f)
 Define a value with a weight and a callback. More...
 
int random ()
 Choose a random number and execute the callback. More...
 

Detailed Description

Weighted random generator with callbacks.

Example code

// Create a weighted random generator
math::WeightedRandom wr;
// Define the entry '0' with a weight of 5, will write "0 choosed !" if choosed.
wr.define(0, 5, [](){ Log.Info("0 choosed !") });
// Define the entry '1' with a weight of 2, will write "1 choosed !" if choosed.
wr.define(1, 2, [](){ Log.Info("1 choosed !") });
// Define the entry '2' with a weight of 8, will write "2 choosed !" if choosed.
wr.define(2, 8, [](){ Log.Info("2 choosed !") });
// Choose a number between the entries defined, execute the corresponding callback, and return the value choosed.
int choosed = wr.random(); // or only wr.random();

Definition at line 52 of file random.hpp.

Member Function Documentation

void hx3d::math::WeightedRandom::define ( int  i,
int  weight,
std::function< void()>  f 
)

Define a value with a weight and a callback.

Parameters
iValue
weightWeight
fCallback

Definition at line 32 of file random.cpp.

int hx3d::math::WeightedRandom::random ( )

Choose a random number and execute the callback.

Returns
Choosed value

Definition at line 42 of file random.cpp.


The documentation for this class was generated from the following files: