The problem with your code is here:
for(int i=0;i<len;i++)
{
srand ( time(NULL) );
cout <<(test[i]=rand()%10);
}
The issue is that on each iteration of the loop, you're reseeding the randomizer with the current system time, usually measured at second precision. Every time you provide a seed to the randomizer it resets the sequence of random values that gets handed back, and so if you keep seeding the randomizer with the same value over and over and then only call rand
once every time you reseed it, you'll get back the same random number sequence over and over again. Since the system time only changes once a second, this will produce the same sequence of numbers repeatedly.
To fix this, move the call to srand
out of the loop, as seen here:
srand ( time(NULL) );
for(int i=0;i<len;i++)
{
cout <<(test[i]=rand()%10);
}
This way, you only seed the randomizer once, and so your sequence should look more pseudorandom. You may still see some duplicate values, but that will be due to chance rather than the random number generator acting deterministically.