DanS DanS -4 years ago 53
C++ Question

Change parameters of random number generator

I am using the

to generate random velocity values and I want to change the mean and variance in response to user input.

I am using the following:

typedef boost::mt19937 RNG;
static RNG rng();

typedef boost::normal_distribution<double> DIST;
DIST dist_east(vel_e, sigma);
DIST dist_north(vel_n, sigma);

boost::variate_generator<RNG, DIST> east(rng, dist_east);
boost::variate_generator<RNG, DIST> north(rng, dist_north);

velocity.east = east();
velocity.north = north();

My problem is that I only get one value returned from the two variate generators each time it gets called. The values change when I change vel_e, vel_n or sigma but otherwise, I get the same value returned.

I tried making the dist_east, dist_north, east and north objects static but I can't change the parameters after construction.

Is there a way of achieving what I want?

Answer Source

In my opinion the quickest way is just to have a normal distribution with sigma 1 and mean 0. In that way, you can get values from any normal distribution just multiplying for your new sigma and adding the mean.

y = mean + sigma * x
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download