The goal is to create a function that return the first n integers form rand()
with each iteration of the loop having a different rand()
result
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX 1000
// takes length and pointer to the integer
void randomNum(int l, int * num )
{
char str[MAX];
srand(time(0));
sprintf(str , "%d", rand());
char new[l];
for(int i = 0; i< l; i++)
{
new[i] = str[i];
}
new[l] = '\0';
printf("Size : %ld\n", strlen(str));
printf("Original number : %s\n",str);
printf("Final result : %s\n",new);
}
int main()
{
int num;
for (int j =0; j < 10; j++)
{
randomNum(1 , &num);
}
return 0;
}
when compiling this code and running it it generates the same result 10 times, why does this happen when the function randomNum
has srand
in it that is supposed to make rand
generate a different value each time