集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 1595|回复: 3

关于$random的使用问题

[复制链接]
CHA 发表于 2010-6-28 00:13:35 | 显示全部楼层 |阅读模式
在$random(seed)中种子变量seed,设置为1和2,有什么不一样
我想知道,通过设置种子,我能不能预先知道产生的随机序列是多少?
这个种子和随机序列的产生总有一些关系吧?
CCIE 发表于 2010-6-28 00:23:19 | 显示全部楼层
不是吧,每人肯帮忙?自己******!!
VVC 发表于 2010-6-28 01:23:41 | 显示全部楼层
It is quite hard to explain.&nbsp;&nbsp;Basically, $random is a pseudo random sequence.&nbsp;&nbsp;Yes, once the seed is known, you can 预先知道产生的随机序列.<br>
<br>
Upon callling, $random(seed) task modifies the seed variable (seed "output" value is different than seed "input" value).&nbsp;&nbsp;To get to the next vaule in the 随机序列, you can either call $random(seed) or just $random().&nbsp;&nbsp;If you don't provide the seed in your next call, it resue the seed output value from your last call.&nbsp;&nbsp;Verilog-2001 does not support "per-thread" randomization, and sometimes people uses this feature (using the seed "output" value to feed next seed "input") to emulate "per-thread" randomization.<br>
<br>
Here is an example:<br>
<br>
&nbsp; &nbsp;initial begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;seed = 1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;$display("\nseed %0d random sequence", seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;for (int i=0; i&lt;10; i++) begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$write("before calling seed=%d, ", seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;random_val = $random(seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$display("after calling seed=%d, random value=%d", seed, random_val);<br>
&nbsp; &nbsp;&nbsp; &nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;seed = 2;<br>
&nbsp; &nbsp;&nbsp; &nbsp;$display("\nseed %0d random sequence", seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;for (int i=0; i&lt;10; i++) begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$write("before calling seed=%d, ", seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;random_val = $random(seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$display("after calling seed=%d, random value=%d", seed, random_val);<br>
&nbsp; &nbsp;&nbsp; &nbsp;end&nbsp; &nbsp;&nbsp; &nbsp;<br>
<br>
&nbsp; &nbsp;&nbsp; &nbsp;seed = 1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;$display("\nseed %0d random sequence", seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;for (int i=0; i&lt;10; i++) begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$write("before calling seed=%d, ", seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;random_val = $random(seed);<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$display("after calling seed=%d, random value=%d", seed, random_val);<br>
&nbsp; &nbsp;&nbsp; &nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;$finish;<br>
&nbsp; &nbsp;end<br>
&nbsp; &nbsp;<br>
Here is the output:<br>
# run -all <br>
# <br>
# seed 1 random sequence<br>
# before calling seed=&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 1, after calling seed=&nbsp; &nbsp;&nbsp; &nbsp;69070, random value=-2147414528<br>
# before calling seed=&nbsp; &nbsp;&nbsp; &nbsp;69070, after calling seed=&nbsp;&nbsp;475628535, random value=-1671855048<br>
# before calling seed=&nbsp;&nbsp;475628535, after calling seed=-1017563188, random value= 1129920902<br>
# before calling seed=-1017563188, after calling seed=&nbsp;&nbsp;772999773, random value=-1374483364<br>
# before calling seed=&nbsp;&nbsp;772999773, after calling seed= -417135238, random value= 1730349006<br>
# before calling seed= -417135238, after calling seed= -473131853, random value= 1674352583<br>
# before calling seed= -473131853, after calling seed= 1662200408, random value= -485282618<br>
# before calling seed= 1662200408, after calling seed= 2044158073, random value= -103324941<br>
# before calling seed= 2044158073, after calling seed= -505977370, random value= 1641506755<br>
# before calling seed= -505977370, after calling seed=&nbsp;&nbsp;797919023, random value=-1349564321<br>
# <br>
# seed 2 random sequence<br>
# before calling seed=&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 2, after calling seed=&nbsp; &nbsp;&nbsp;&nbsp;138139, random value=-2147345408<br>
# before calling seed=&nbsp; &nbsp;&nbsp;&nbsp;138139, after calling seed=&nbsp;&nbsp;951188000, random value=-1196295055<br>
# before calling seed=&nbsp;&nbsp;951188000, after calling seed= 1784212385, random value= -363270956<br>
# before calling seed= 1784212385, after calling seed=-1731404562, random value=&nbsp;&nbsp;416079665<br>
# before calling seed=-1731404562, after calling seed=-1607270249, random value=&nbsp;&nbsp;540214080<br>
# before calling seed=-1607270249, after calling seed= -529128468, random value= 1618355648<br>
# before calling seed= -529128468, after calling seed= -497434627, random value= 1650049476<br>
# before calling seed= -497434627, after calling seed=-1868851558, random value=&nbsp;&nbsp;278632737<br>
# before calling seed=-1868851558, after calling seed= 1238854483, random value= -908628845<br>
# before calling seed= 1238854483, after calling seed= 2101815416, random value=&nbsp;&nbsp;-45667590<br>
# <br>
# seed 1 random sequence<br>
# before calling seed=&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 1, after calling seed=&nbsp; &nbsp;&nbsp; &nbsp;69070, random value=-2147414528<br>
# before calling seed=&nbsp; &nbsp;&nbsp; &nbsp;69070, after calling seed=&nbsp;&nbsp;475628535, random value=-1671855048<br>
# before calling seed=&nbsp;&nbsp;475628535, after calling seed=-1017563188, random value= 1129920902<br>
# before calling seed=-1017563188, after calling seed=&nbsp;&nbsp;772999773, random value=-1374483364<br>
# before calling seed=&nbsp;&nbsp;772999773, after calling seed= -417135238, random value= 1730349006<br>
# before calling seed= -417135238, after calling seed= -473131853, random value= 1674352583<br>
# before calling seed= -473131853, after calling seed= 1662200408, random value= -485282618<br>
# before calling seed= 1662200408, after calling seed= 2044158073, random value= -103324941<br>
# before calling seed= 2044158073, after calling seed= -505977370, random value= 1641506755<br>
# before calling seed= -505977370, after calling seed=&nbsp;&nbsp;797919023, random value=-1349564321<br>
# ** Note: $finish&nbsp; &nbsp; : test.sv(211)
ICE 发表于 2010-6-28 02:16:17 | 显示全部楼层
谢谢liaohe的例子,我好好研究一下
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|fpga论坛|fpga设计论坛 ( 京ICP备20003123号-1 )

GMT+8, 2024-11-27 11:04 , Processed in 0.061136 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表