In a simulation I'm writing I have a class that represents an Agent that must take some actions and I want this agent to have access to a random number generator. I heard boost rng's where good ones, so I wanted to learn how to use them.
So, here's the problem. This code compiles and runs perfectly:
//Random.cpp
#include <boost/random.hpp>
#include <boost/limits.hpp>
#include <iostream>
#include <ctime>
int main()
{
int N = 10;
boost::lagged_fibonacci607 rng;
rng.seed(static_cast<boost::uint32_t> (std::time(0)));
boost::uniform_real<> uni(0.0,1.0);
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> > uniRng(rng, uni);
for (int i = 0; i < N; ++i)
std::cout << uniRng() << std::endl;
return 0;
}
So, I wanted my Agent class to have access to have a private object of type:
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> >
that can be called when the agent needs a random number.
So what I tried to do is:
//RandomAgent.cpp
#include <boost/random.hpp>
#include <boost/limits.hpp>
#include <iostream>
#include <ctime>
class Agent {
private:
boost::lagged_fibonacci607 rng;
boost::uniform_real<> uni(0.0,1.0);
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> > uniRng(rng, uni);
public:
Agent() {
rng.seed(static_cast<boost::uint32_t> (std::time(0)));
}
void show() {
std::cout << uniRng() << std::endl;
}
};
int main() {
Agent foo;
for(int i = 0; i < 10; ++i)
foo.show()
}
And I got the following error messages:
$ g++ RandomAgent.cpp
Random.cpp:10: error: expected identifier before numeric constant
Random.cpp:10: error: expected ‘,’ or ‘...’ before numeric constant
Random.cpp:11: error: ‘rng’ is not a type
Random.cpp:11: error: ‘uni’ is not a type
Random.cpp: In member function ‘void Agent::show()’:
Random.cpp:18: error: no matching function for call to ‘Agent::uniRng()’
Random.cpp:11: note: candidates are: boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real
> Agent::uniRng(int, int)
I think you need to initialise you member variables uni
and uniRng
in the constructor initialisation list rather than inline where they are declared.