Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly set a random number generator?

In order to develop my implementation of the particle filter algorithm, I need to generate hypotheses about the movements relating to the object to be tracked: if I set N samples and if I use a 2-by-1 state vector, then at each step I have to generate N pairs of random values (a 2-by-N matrix). Moreover, if I know the statistics of movements (mean and standard deviation), then I could use the mean and standard deviation to generate all N values. Finally, to model the uncertainty of the movement, I could generate a noise matrix (a 2-by-N matrix) and add it to the matrix of movements.

Based on these premises, I have implemented the algorithm running in matlab, and I used the following code in order to generate the hypotheses of movement.

ds_mean = [dx_mean dy_mean];
ds_stddev = [dx_stddev dy_stddev];
d = 5;

V = zeros(2,N);
V(1,:) = normrnd(ds_mean(1),ds_stddev(1),1,N);   % hypotheses of movement on x axis
V(2,:) = normrnd(ds_mean(2),ds_stddev(2),1,N);   % hypotheses of movement on y axis

E = d*randn(2,N);   % weighted noise

M = V + E;   % hypotheses of movement

A problem occurred when I had to implement the same algorithm using C++ and OpenCV: substantially, while the above matlab code generates good predictions (it works great), instead the same code written in C++ (see the code below) generates poor predictions (ie far away from the object). Why?

RNG m_rng;

x_mean = // ...
y_mean = // ...
x_stddev = // ...
y_stddev = // ...

Mat velocity(STATE_DIM, NUM_PARTICLES, DataType<double>::type);
m_rng.fill(velocity.row(0), RNG::NORMAL, x_mean, x_stddev);
m_rng.fill(velocity.row(1), RNG::NORMAL, y_mean, y_stddev);

Mat noise(STATE_DIM, NUM_PARTICLES, DataType<double>::type);
m_rng.fill(noise,RNG::NORMAL,0,1);
noise *= d;   % weighted noise

movements = velocity + noise;

How to make sure that the C++ algorithm works as well as the algorithm implemented in matlab?

like image 225
enzom83 Avatar asked Feb 03 '26 18:02

enzom83


1 Answers

I think I just serendipitously answered your question here, or at least provided an alternative solution.

https://stackoverflow.com/a/13897938/1899861

I believe this will generate proper random numbers, and has been tested to death when compiled using Microsoft C on Intel processors (386, 486, Pentium).

FYI, 4.0 * atan(1.0) yields a much better value of PI than the constant in the above environment.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!