- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
--- 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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page