I have created a method that returns a string as per the value of generated random number. This work as expected when the function is in the same program file where it is called.
But when I define this method in a new header file and then call it in a different file then it is returning same random number.
Defined method:
char* newTrafficGenerator(void)
{
int i;
int count =0;
static char currentTrafficType[6];
i = random_rand();
printf("Current random no is: %d\n",i);
if (i>0 && i<32767)
{
strncpy(currentTrafficType,"Type A",sizeof(currentTrafficType));
}
else if(i>32767 && i<55705)
{
strncpy(currentTrafficType,"Type B",sizeof(currentTrafficType));
}
else
{
strncpy(currentTrafficType,"Type C",sizeof(currentTrafficType));
}
return currentTrafficType;
}
Called by following code:
static void generateInfiniteRandomNumber(void)
{
int count=0;
char *c;
while(count<10)
{
c = newTrafficGenerator();
printf("Type is %s\n",c);
count++;
}
}
The output when called in same file is:
Current random no is: 18547
Type is Type A
Current random no is: 56401
Type is Type C
Current random no is: 23807
Type is Type A
Current random no is: 37962
Type is Type B
Current random no is: 22764
Type is Type A
Current random no is: 7977
Type is Type A
Current random no is: 31949
Type is Type A
Current random no is: 22714
Type is Type A
Current random no is: 55211
Type is Type B
Current random no is: 16882
Type is Type A
When defined in a new header file and then output upon calling is:
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C
Current random no is: 1920540202
Type is Type C