I have the code below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void random_num_generator(int *arr_ptr, int num_of_elements, int start_val, int end_val){
srandom(time(NULL));
int i;
for(i = 0; i < num_of_elements; i++){
*(arr_ptr+i) = start_val + random()/(RAND_MAX / (end_val - start_val) + 1);
}
}
int main(){
int arr_size = 10;
int *test1 = (int*)malloc(arr_size*sizeof(int));
int *test2 = (int*)malloc(arr_size*sizeof(int));
random_num_generator(test1, arr_size, 0, 50);
random_num_generator(test2, arr_size, 0, 50);
int i;
for(i = 0; i < arr_size; i++){
printf("%d ",*(test1+i));
}
printf("\n");
for(i = 0; i < arr_size; i++){
printf("%d ",*(test2+i));
}
printf("\n");
free(test1);
free(test2);
return 0;
}
So, the problem is whenever I call call the random_num_generator() function, it should give me different sequences of numbers. But, here in this code I call it two times and it gives me the same exact set of sequence both times.
RUN 1 OUTPUT
24 18 7 3 21 10 14 23 29 24
24 18 7 3 21 10 14 23 29 24
RUN 2 OUTPUT
30 22 13 23 23 7 27 37 16 39
30 22 13 23 23 7 27 37 16 39
As you can see, the program only generates a new sequence of random characters whenever I terminate the program and re-run it.
Can someone please explain this to me. Thank you.
Aucun commentaire:
Enregistrer un commentaire