2015年5月5日 星期二

Verilog Random Seed Generation

Random Seed Generation in Verilog

在Verilog中有system task $random來產生亂數,
其需要一個亂數種子才能讓每次產生的亂數不同.
以下使用時間產生亂數種子的方法.
產生的NowInSeconds帶入$random(NowInSeconds),
則每次可得不同的亂數.



From : http://www.edaboard.com/thread75097.html

integer FP;
integer fgetsResult;
integer sscanfResult;
integer NowInSeconds;
reg [8*10:1] str;

// call "date" and put out time in seconds since Jan 1, 1970 (when time began, no doubt)
// and put the results in a file called "now_in_seconds"
$system("date +%s > now_in_seconds");                                                   

// open the file for reading
FP = $fopen("now_in_seconds","r");

// get a string from the open file - "fgetsResult" should be a 1 - you can test 
// that for completeness if you'd like
fgetsResult = $fgets(str,FP);

// convert the string to an integer - "sscanfResult" should also be a 1, and
// you can test that, too, 
sscanfResult = $sscanf(str,"%d",NowInSeconds);

// close the file...
$fclose(FP);  // closes the file

// use the number as a seed...
process::self.srandom(NowInSeconds);

沒有留言:

張貼留言