This question already has an answer here:
- srand() — why call it only once? 7 answers
- random numbers and multiple srand calls 3 answers
- rand with seed does not return random if function looped 5 answers
When I compiled this code, I encountered the same numbers which are "num" times.But I want to take different random numbers.How can I fix it?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int create_random(int num);
int main()
{
int i=0,num=0;
printf("How many random number ? ");//Request from user..
scanf("%d",&num);
for(i=0 ; i<num ; ++i)
{
printf("%d\n",create_random(i));
}
return 0;
}
int create_random(int a)
{
a=0;
srand(time(NULL));
a=rand()%100;
return a;
}
Aucun commentaire:
Enregistrer un commentaire