I have written the following piece of code to generate a random sequence of certain numbers {0,1,2,...,31}. It works fine, however, it cannot be guaranteed to complete within any finite amount of time; after any interval there is still only a certain (very high) probability that it will have completed. Any suggestion for removing this issue?
int th;
vector<int> V2 = vector<int> (32,0);
for (int k=0;k<32;k++){
do{
th = rand() % 32;
} while ( V2[th] == 0 );
V2[th] = k;
}