Altera_Forum
Honored Contributor
9 years agosrand with time(NULL) as argument not working in Nios
I need to run some heuristics using Nios, but time(NULL) is always returning the same value.
This fact can been detected using some code as this one below. Each time I run this code, the sequence becomes pseudo-aleatory, i.e., the same results are produced because srand received the same seed. In a computer, time(NULL) always returns a diferent number.# include <stdio.h># include <stdlib.h># include <time.h>
# define SIZE 10
int main()
{
int i, j;
int my_array;
srand(time(NULL));
for(i=0; i<SIZE; i++){
for(j=0; j<SIZE; j++){
my_array = rand()%SIZE;
printf(" %d ", my_array);
}
printf("\n");
}
return 0;
}
This is not my specific code. I only show this to demonstrate more easily my problem. How can I produce diferent seeds to my srand argument?