Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12618 Discussions

srand with time(NULL) as argument not working in Nios

Altera_Forum
Honored Contributor II
2,135 Views

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
Altera_Forum
Honored Contributor II
773 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
Altera_Forum
Honored Contributor II
773 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
Reply