Your loop:
|
for( int m = 0; m <= i; ++m )
| |
is checking not only all previously filled out elements, but also the current one you are looking
to fill out. You don't want to check the current one, so
m <= i;
should be
m < i;
.
But... there are not only easier ways, but also more efficient ways. Consider pre-filling the array
with the numbers 1-52 in order, then, in a loop, randomly choosing two elements to swap and
swapping them. If you repeat the swap a bunch of times, the effect will be to randomize the
ordering, which is what you want.
If this is not a homework assignment, then consider using the std::random_shuffle() algorithm, as
it does the random swap loop for you.
1 2 3
|
#include <algorithm>
std::random_shuffle( &num[0], &num[length], rand );
| |
Ought to do the trick.