0

I am currently looking into new c++11 random library. For simplification, I created following class

class my_rand {
    private:
        std::mt19937_64 eng;
    public:
        my_rand() {
            std::array<int, std::mt19937::state_size> seed_data;
            std::random_device rd;
            std::generate_n(seed_data.data(), seed_data.size(), std::ref(rd));
            std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
            this->eng.seed(seq);
        };
        unsigned long long operator() () {
            return this->eng();
        };
};

so that I can just do my_rand rand; once and then repeatedly call rand() to generate random numbers.

However, now I want to use std::uniform_int_distribution to set bound to numbers I get. It seams obvious to specify bounds while creating the object, so that invocations stay the same. The problem is, if I add private member (something like std::uniform_int_distribution<long long> uid;) then later in constructor I will not be able to set bounds because uid will already be initialized. I could not find method of std::uniform_int_distribution which allowed to set limits after it is created.

I may well be missing some point here, so thanks in advance for any help.

Cthulhu
  • 1,379
  • 1
  • 13
  • 25

1 Answers1

1

Use initializer lists to construct member variables or base classes. (See here for more info)

my_rand(...arguments...) : uid(...arguments...) //<-- Initializer list
{
    //...stuff...
} //<-- No semicolon after function definition.

If that doesn't suit your needs, you can always go like this:

myVar = int(27);

For example:

//Initial construction:
std::uniform_int_distribution<> testA(1, 6);

//Re-assignment by copy-construction (move-construction actually)
//and a temporary object.
testA = std::uniform_int_distribution<>(5, 10);
Community
  • 1
  • 1
Jamin Grey
  • 10,151
  • 6
  • 39
  • 52
  • Yeah about that, extra semicolons are not restricted yet it seams more consistent with class declarations. I know that class declaration can be followed by objects declarations and function cannot, but still... – Cthulhu Jul 08 '13 at 06:09
  • That won't work for you when using lambdas - the extra semicolons will cause compile errors there. =) – Jamin Grey Jul 08 '13 at 06:11
  • Oh, thanks for warning. Did not look into that area yet. – Cthulhu Jul 08 '13 at 06:16