#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sep_gen(void);
int main()
{
srandom((unsigned)time(NULL));
printf("Random Numbers\n");
sep_gen();
sep_gen();
sep_gen();
return(0);
}
void sep_gen(void)
{
long int r;
int i;
for (i=0;i<10;i++)
putchar('-');
putchar('\n');
r=random();
printf("%ld\n",r);
}
The above code generates three different integers as intended. However, when the srandom((unsigned)time(NULL)) is used sep_gen() function, the integers generated are the same all the three times. (Code below)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sep_gen(void);
int main()
{
printf("Random Numbers\n");
sep_gen();
sep_gen();
sep_gen();
return(0);
}
void sep_gen(void)
{
srandom((unsigned)time(NULL));
long int r;
int i;
for (i=0;i<10;i++)
putchar('-');
putchar('\n');
r=random();
printf("%ld\n",r);
}
My question why is that random() function generates different numbers when the srandom() functioned is placed in the main function, but doesn't do so when in sep_gen() function?
Aucun commentaire:
Enregistrer un commentaire