I have recently finished coding a random walks type program. In order to generate the randomness I used a time-seeded call of rand(). Now for the most part this seems to work fine, but every so often a loop of mine seems to be caught in what is almost an infinite loop. Is it normal for a loop containing rand() to take so long to generate a restricted random integer? Here is my code....

Code:#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void){ int Horiz=0,Vert=0,BoolWplane[10][10], pointset=0, initpointh=0, initpointv=0, j=0, blockwall[4], right=0, up=0, sum=0; char Walkplane[10][10],walkpos='A'; int direcprim=0; for(Horiz=0;Horiz<10;Horiz++){ for(Vert=0;Vert<10;Vert++){ Walkplane[Horiz][Vert]='.'; BoolWplane[Horiz][Vert]=0; } } srand((unsigned) time(NULL)); while(pointset!=1){ if(j==0){ initpointh=rand()%10; initpointv=rand()%10; j++; } if(((initpointh!=0)&&(initpointh!=9))||((initpointv!=0)&&(initpointv!=9))) j--; else pointset=1; } Horiz=initpointh; Vert=initpointv; BoolWplane[Horiz][Vert]=1; Walkplane[Horiz][Vert]=walkpos; initpointv=0; while(walkpos<='Y'&&sum!=4){ for(initpointh=0;initpointh<4;initpointh++) blockwall[initpointh]=0; start: if(initpointv>0){ blockwall[direcprim]=1; sum=0; } up=0; right=0; direcprim=rand()/(RAND_MAX/5); switch(direcprim){ case 0: up++; break; case 1: up--; break; case 2: right++; break; case 3: right--; break; } if(((Horiz+right)>9)||((Horiz+right)<0)||((Vert+up)<0)||((Vert+up)>9)){ continue; } if(BoolWplane[Horiz+right][Vert+up]==1){ blockwall[direcprim]=1; for(initpointh=0;initpointh<4;initpointh++){ if(blockwall[initpointh]==1){ sum++; } } if(sum==4) { printf("sum is 4\n"); continue; } else goto start; } Horiz=Horiz+right; Vert=Vert+up; BoolWplane[Horiz][Vert]=1; Walkplane[Horiz][Vert]=++walkpos; initpointv++; } for(Horiz=0;Horiz<10;Horiz++){ for(Vert=0;Vert<10;Vert++){ printf(" %c",Walkplane[Horiz][Vert]); } printf("\n"); } return 0; }