Ask Your Question
1

Why are the results of RNG same every time?

asked 2013-11-11 21:47:45 -0600

zchenkui gravatar image

updated 2015-12-29 20:14:37 -0600

Hello. I have a problem about RNG class. I want to get different point randomly from a given image, so I use the RNG class which is recommanded in the OpenCV document. The code is:

struct SingleAnt
{
    int row;
    int col;
};
void initializeAnts( SingleAnt *ants, Mat *sourceImage )
{
    RNG rng( 0xFFFFFFFF );
    int imgWidth = sourceImage->cols;
    int imgHight = sourceImage->rows;

    for( int index = 0; index < ANTSNUMBER; index++ ) {
        ants[ index ].col = rng.uniform( 0, imgWidth );
        ants[ index ].row = rng.uniform( 0, imgHight );
    }
}

But whenever I run this code, I get the same result. Are there any mistakes in the code? Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-11-12 06:39:07 -0600

Geppertm gravatar image

The Problem is that your are initializing RNG always with the same value. Try this:

#include <time.h>
.
.

void initializeAnts( SingleAnt *ants, Mat *sourceImage )
{
    uint64 initValue = time(0);
    RNG rng( initValue );
    int imgWidth = sourceImage->cols;
    int imgHight = sourceImage->rows;

    for( int index = 0; index < ANTSNUMBER; index++ ) {
        ants[ index ].col = rng.uniform( 0, imgWidth );
        ants[ index ].row = rng.uniform( 0, imgHight );
    }
}

This should give you different random values.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-11-11 21:47:45 -0600

Seen: 655 times

Last updated: Nov 12 '13