STL function random_shuffle() possible bug?

R

Ronak

Hi,
I'm encountering a very erratic behavior of STL function
random_shuffle() in my code. I have vectors s1 and s2 each having
elements {1, 2, 3, 4, 5, 6} which i want to randomly shuffle.

My problem is that random_shuffle() (with or without the third
parameter) replaces any element of either vector with 0. for example
after some random_shuffles, i get

Sequence S1: { 1 5 2 6 4 0 }, Sequence S2: { 1 3 5 4 2 6 }
OR
Sequence S1: { 3 2 5 6 4 0 }, Sequence S2: { 4 5 3 2 6 1 }

This is the code I wrote to do random_shuffle()

void gen_new_sequence () {

MyRandom rd;
srand((unsigned)time( NULL));
random_shuffle(s1.begin(), s1.end( ), rd);

srand((unsigned)time( NULL ));
random_shuffle( s2.begin( ), s2.end( ), rd);
}

The function object MyRandom is defined as follows:

class MyRandom { //random number generating functor
public:
ptrdiff_t operator() (ptrdiff_t max) {
double tmp;
tmp = static_cast<double>(rand()) /
static_cast<double>(RAND_MAX);
return static_cast<ptrdiff_t>(tmp * max);
}
};

One thing i noted is that if vectors s1 and s2 have a large size, the
problem doesn't occur. For smaller sized vectors, a 0 gets invariably
inserted somewhere.

Can anyone help?
 
T

tom_usenet

Hi,
I'm encountering a very erratic behavior of STL function
random_shuffle() in my code. I have vectors s1 and s2 each having
elements {1, 2, 3, 4, 5, 6} which i want to randomly shuffle.

random_shuffle is a simple algorithm. You should be able to tell by
inspection of the source code that it is correct - I severely doubt
such a simple algorithm would contain a bug.
My problem is that random_shuffle() (with or without the third
parameter) replaces any element of either vector with 0. for example
after some random_shuffles, i get

Sequence S1: { 1 5 2 6 4 0 }, Sequence S2: { 1 3 5 4 2 6 }
OR
Sequence S1: { 3 2 5 6 4 0 }, Sequence S2: { 4 5 3 2 6 1 }

This is the code I wrote to do random_shuffle()

void gen_new_sequence () {

MyRandom rd;
srand((unsigned)time( NULL));
random_shuffle(s1.begin(), s1.end( ), rd);

srand((unsigned)time( NULL ));

You should only generally call srand once in your whole program (often
the first line of main).
random_shuffle( s2.begin( ), s2.end( ), rd);
}

The function object MyRandom is defined as follows:

class MyRandom { //random number generating functor
public:
ptrdiff_t operator() (ptrdiff_t max) {
double tmp;
tmp = static_cast<double>(rand()) /
static_cast<double>(RAND_MAX);

The above line is wrong, since rand() == RAND_MAX is possible. It
should be:

tmp = rand() / (RAND_MAX + 1.0);

return static_cast<ptrdiff_t>(tmp * max);
}
};

One thing i noted is that if vectors s1 and s2 have a large size, the
problem doesn't occur. For smaller sized vectors, a 0 gets invariably
inserted somewhere.

Can anyone help?

The random functor has to behave as specified - it must return a
number in the range [0, max). Yours was returning [0, max] (e.g.
inclusive at both ends). I suspect that this is the problem, but it
might not be.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top