I'm trying to generate random number between 0 and 40(inclusive). So the code I Implemented is this-
y=rand()%41;
However everytime I click compile and hit Run. It outputs the same random numbers. As in say for instance I ran this in a loop.
for(i=0;i<4;i++)
{
y=rand()%41;
printf("%d ",y);
}
Every single time, the output is the same 4 numbers. It always outputs 14,2,etc etc on the terminal. No matter what.
So my first question is, why is this happening?
and Secondly, how does the random number generator work in C?
I thought since I include time.h library, the numbers are generated through some standard algorithm using the system time. And since the system time is continuously changing, the numbers that are generated should also change every time I run the program.