Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a C++11 random number generator to a function?

Tags:

c++

random

c++11

Do they all inherit from a base class? Do I have to use templates?

(I am referring to these http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15319/)

I am doing this right now:

typedef std::mt19937 RNG;

and then

class Chooser {
public:
    Chooser(RNG& rng, uint n, uint min_choices, uint max_choices):

In other words, I'm passing references to RNG. How would I pass in an arbitrary generator?

Also, I realize this is maybe a different question, but how do I pass the generator to STL?

std::random_shuffle(choices_.begin(), choices_.end(), rng);

doesn't seem to work.


solution to passing generator:

typedef std::ranlux64_base_01 RNG;
typedef std::mt19937 RNGInt;

solution to passing to STL:

struct STL_RNG {
    STL_RNG(RNGInt& rng): gen(rng) {}       
    RNGInt& gen;
    int operator()(int n) { return std::uniform_int<int>(0, n)(gen); }
};
like image 932
Neil G Avatar asked Feb 11 '10 00:02

Neil G


People also ask

How do you generate a random number between 1 to 100 in C++?

Similar to 1 and 10, you can generate random numbers within any range using the modulus operator. For instance, to generate numbers between 1 and 100, you can write int random = 1+ (rand() % 100). You can represent the general syntax of the equation as: int random = offset + (rand() % range);

How do you generate a random number between 10 and C++?

rand() function generates random number and when you use rand()%10 , it will give you last digit of the number. Since we require random number between 1 and 10, we have used rand()%10+1 to generate random number between 1 and 10 in C++.


1 Answers

They don't all inherit from a base (which is a little surprising), but it doesn't matter because that's not how C++ functors work.

For arbitrary RNGs of a single given type, you got it right as (now) posted.

If you mean, how do I define a function which accepts any random number generator as an argument.

template< class RNG > // RNG may be a functor object of any type
int random_even_number( RNG &gen ) {
    return (int) gen() * 2;
}

You don't need to use any more templates than this, because of type deduction.


Defining one function to accept different RNG's is trickier because semantically that requires having a common base type. You need to define a base type.

struct RNGBase {
    virtual int operator() = 0;
    virtual ~RNGBase() {};
};

template< class RNG >
struct SmartRNG : RNGBase {
    RNG gen;

    virtual int operator() {
        return gen();
    }
};

int random_even_number( RNGBase &gen ) { // no template
    return (int) gen() * 2; // virtual dispatch
}
like image 91
Potatoswatter Avatar answered Oct 04 '22 00:10

Potatoswatter