Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Valued Contributor III
1,371 Views

srand 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?
0 Kudos
2 Replies
Highlighted
Valued Contributor III
9 Views

It's going to depend on your system/environment. 

 

To use time() like you would like to, you would need at least a timer and to have set it with time of day information somehow (NTP, GPS, an RTC, manual entry, ...). 

 

"computers" only use time() as a seed because it's nearly the only thing they have in common with each other so software can easily move from one computer to another and still work. 

 

In your FPGA design, look for other sources of random data to use as your seed. 

 

If you're using a compatible device, if nothing else you may want to consider the Altera Random Number Generator IP Core 

https://www.altera.com/en_us/pdfs/literature/ug/ug-random.pdf
0 Kudos
Highlighted
Valued Contributor III
9 Views

 

--- Quote Start ---  

It's going to depend on your system/environment. 

 

To use time() like you would like to, you would need at least a timer and to have set it with time of day information somehow (NTP, GPS, an RTC, manual entry, ...). 

 

"computers" only use time() as a seed because it's nearly the only thing they have in common with each other so software can easily move from one computer to another and still work. 

 

In your FPGA design, look for other sources of random data to use as your seed. 

 

If you're using a compatible device, if nothing else you may want to consider the Altera Random Number Generator IP Core 

https://www.altera.com/en_us/pdfs/literature/ug/ug-random.pdf 

--- Quote End ---  

 

 

Thanks ted
0 Kudos