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?
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?