Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vary range of uniform_int_distribution

So i have a Random object:

typedef unsigned int uint32;  class Random { public:     Random() = default;     Random(std::mt19937::result_type seed) : eng(seed) {}  private:     uint32 DrawNumber();     std::mt19937 eng{std::random_device{}()};     std::uniform_int_distribution<uint32> uniform_dist{0, UINT32_MAX}; };  uint32 Random::DrawNumber() {     return uniform_dist(eng); } 

What's the best way I can vary (through another function or otherwise) the upper bound of of the distribution?

(also willing to take advice on other style issues)

like image 555
LordAro Avatar asked Sep 26 '13 18:09

LordAro


2 Answers

Distribution objects are lightweight. Simply construct a new distribution when you need a random number. I use this approach in a game engine, and, after benchmarking, it's comparable to using good old rand().

Also, I've asked how to vary the range of distribution on GoingNative 2013 live stream, and Stephen T. Lavavej, a member of the standard committee, suggested to simply create new distributions, as it shouldn't be a performance issue.

Here's how I would write your code:

using uint32 = unsigned int;  class Random { public:     Random() = default;     Random(std::mt19937::result_type seed) : eng(seed) {}     uint32 DrawNumber(uint32 min, uint32 max);  private:             std::mt19937 eng{std::random_device{}()}; };  uint32 Random::DrawNumber(uint32 min, uint32 max) {     return std::uniform_int_distribution<uint32>{min, max}(eng); } 
like image 164
Vittorio Romeo Avatar answered Oct 09 '22 22:10

Vittorio Romeo


You can simply create a std::uniform_int_distribution<uint32>::param_type and modify the range using the param() method. You can cut down the template noise with decltype :

decltype(uniform_dist.param()) new_range (0, upper); uniform_dist.param(new_range); 
like image 40
Brett Hale Avatar answered Oct 09 '22 21:10

Brett Hale