So, my c program will print out how deep Binary search tree is after inserting N random numbers, I wanted to run it 500 times and save the result in the text file.
After compiling, gcc -Wall myProgram main.c.
I tried ./myProgram and it returns different result every time which is
9012,9023,9231,9523,9533...
, but as soon as I use bash loop
for i in {1..500} do ./myProgram >> text.txt done
It always returns 500 same number. I initially thought there is problem with my C program, but when I run it on IDE or just running it once works without a problem
I forgot to mention that my c program uses time.h for random number generation, could it be the problem?
srand(time(NULL));
T = Initialize();
T = MakeEmpty(T);
int array[N];
for (int i = 0; i < N; i++) { // fill array
array[i] = i;
}
for (int k = 0; k < N; k++) { // shuffle array
int temp = array[k];
int randomIndex = rand() % N;
array[k] = array[randomIndex];
array[randomIndex] = temp;
}
for (j = 0; j < N; j++){
//printf("%d\n", array[j]);
T = Insert(array[j], T);
}