printf("This is the %d th iteration\n",i);
printf("random number generated is: %d\n",a);
The problem is with srand(). the random number generator is not random, it is pseufo-random, it means that if you set the seed 2 times with same value, you will get the same 'random' sequence. And since your seed is a number of seconds, you get every time the same sequence.