By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

STL function random_shuffle() possible bug?

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 16 Nov 2003 13:12:57 -0800, ro***********@yahoo.com (Ronak) wrote:
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
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.