These are two programs for generating random 0's and 1's
The essential logic is the same the first one
has the code in main whereas the second one has in the function
randgen().
The first is generating the correct output whereas the second is giving only zeroes.Why the discrepancy?
PROGRAM 1
Code:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
int i;
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
for(i=0;i<100;i++)
{
printf("%d",rand()%2);
}
}
PROGRAM 2
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int randgen()
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
return rand()%2;
}
int main()
{
int i=0;
for(i=0;i<100;i++)
printf("\n %d",randgen());
}