当前位置:网站首页>Verilog's random number system task----$random
Verilog's random number system task----$random
2022-08-02 10:02:00 【lonely single knife】
目录
概述
在做仿真的时候,Inevitably some data will be required as input.Sometimes the input data is required,Any kind of data will do.这种情况下有两种办法:
- Just write some data,But the amount of data is a big trouble,still mind
- 使用VerilogProvided random number generation system task$randomto help generate a lot of random numbers,A system task all done!
$random 是VerilogProvides a random number generation system task,After calling the task,将会返回一个32bit的integerType of the value of the symbol.Its calling format is3种:
- $random;
- $random();
- $random(seed);
$random与$random()
$random与$random()用起来The method and the result are the same,You can write a littleTB测试一下:
`timescale 1ns / 1ns
module random_test();
reg [31:0] rand_data; //定义一个32位数据
//每10ns产生一个随机数
initial begin
rand_data = 0;
repeat(5) begin
#10 rand_data = $random;
end
#5 $finish;
end
endmodule
上面的TBFile to do is simple:每隔10ns生成一个随机数,重复5次.其结果如下:

可以看到生成了5个32bit 的随机数,There are integers and negative numbers.
接下来我们把上面的TB中的系统任务 $random替换成$random()的,其他不变,Look at the simulation results again:

is consistent with the previous results,这说明$random替换成$random()In fact, the effect is the same,所以我们一般都用$random.
$random的返回值是一个32位的整数,But sometimes such a large number is not necessary.If you want the value of the random number to be fixed in a certain range,那么可以这么使用:$random%b;Then the range of generated random numbers is [ ( -b+1 ) : (b- 1 ) ]. 这其实就是对b取余,Isn't that just framing the scope?!
同样的,用上面的TB测试一下,Generate several random numbers at the same time for easy comparison(除数用10,生成范围[-9:9]):
`timescale 1ns / 1ns
module random_test();
reg [31:0] rand_data; //定义一个32位数据
//每10ns产生一个随机数
initial begin
rand_data = 0;
repeat(10) begin
#10 rand_data = $random%10;
end
#5 $finish;
end
endmodule
其结果如下:
![]()
The generated values are范围[-9:9]内(There are several duplicate notice).
此外,If we wish to generate only random numbers in the positive range,那么可以这么使用:{$random}%b;Then the range of generated random numbers is [ 0 : (b - 1 ) ].
还是用上面的TBTest the result of random number generation in the positive range【0:9】的效果.Look at the simulation results:
![]()
嗯很好,The result is already【0:9】范围内的数了.
$random(seed)
先别说其他的,直接用modelsimSimulate the following module:生成10次【0:9】范围内的随机数:
`timescale 1ns / 1ns
module random_test();
reg [31:0] rand_data; //定义一个32位数据
//每10ns产生一个随机数
initial begin
rand_data = 0;
repeat(10) begin
#10 rand_data = {$random}%10;
end
#5 $finish;
end
endmodule把结果记录下来:

然后把modelsim关了(You can even restart the computer),rest with a cappuccino5分钟,Brush Bilibili、摸摸鱼.
然后重新打开modelsim,Re-simulate the above module,record the result:

咦?奇怪?Why are the two simulation results the same??What about the random number generator function??He's not random.?
实际上,$randomNot a real random number generator,If we call it at the same time every simulation, it is actually the seedseed一致,Then the random number generated is consistent.
In the simulation above,我们省略了seedseed parameter,By the simulation tools generated by default,And the simulation tool to generate seeds are set according to the simulation time.So every time we run a simulation,In fact, they are calling the same at the same timeseed,So it is not surprising that the simulation results are consistent..
忘了说,$random中的seed数据类型可以是reg,integer或者time.
接下来,我们改一下TB,加上seed:
`timescale 1ns / 1ns
module random_test();
reg [31:0] rand_data; //定义一个32位数据
//每10ns产生一个随机数
initial begin
rand_data = 0;
repeat(10) begin
#10 rand_data = {$random(1)}%100;
end
#5 $finish;
end
endmodule
第1times we use 1作为seed,看看结果:

Since every time I use1作为seed,所以10The random numbers generated each time are68(0是初始值,不是随机生成的).
把seed改成10,结果如下:
![]()
Since every time I use10作为seed,所以10The random numbers generated each time are48(0是初始值,不是随机生成的).
所以,If you want the best randomness in every simulation,Then it's better to change it every timeseed.
常用用法
The common usage of calling random number generation function in ordinary simulation is:
输入 data_in,位宽【a-1:0】,即位宽a,其值范围2^a,Verilog语法即2**a; //2**a表示2的a次方.
So if you need to simulatedata_in的随机输入,通常这样调用:data_in = {$random}%(2**a);
比如:
input [3:0] data_in; //其值范围为2进制0000~1111(即十进制0-15),
data_in = {$random}%(2**4); //即data_in = {$random}%16),Generate a random number in the range of0-15,Perfect coverage of the full range of input data.
总结与参考
- $random与$random()的用法、结果都是一致的
- $random%brange can be generated [ ( -b+1 ) : (b- 1 ) ]内的随机数
- {$random}%brange can be generated [ 0: (b- 1 ) ]内的随机数
参考资料1:IEEE Standard for Verilog Hardware Description Language(IEEE Std 1364-2005)
- 博客主页:wuzhikai.blog.csdn.net
- 本文由 孤独的单刀 原创,首发于CSDN平台
- 您有任何问题,都可以在评论区和我交流!
- 创作不易,您的支持是我持续更新的最大动力!如果本文对您有帮助,还请多多点赞、评论和收藏!
边栏推荐
- 阿里巴巴 CTO 程立:开源是基础软件的源头!
- 高效时代,电商运营如何靠RPA快速提效?
- 【云原生】快出数量级的性能是怎样炼成的?就提升了亿点点
- One Summer of Open Source | How to Quickly Integrate Log Modules in GO Language Framework
- system_error错误处理库学习
- The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
- The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"
- R language ggplot2 visualization: use the ggtexttable function of the ggpubr package to visualize tabular data (directly draw tabular graphs or add tabular data to images), use tbody_add_border to add
- Facebook自动化数据分析方案,广告投放省心省力
- 瑞吉外卖项目剩余功能补充
猜你喜欢

李航《统计学习方法》笔记之朴素贝叶斯法

理解JS的三座大山

Facebook自动化数据分析方案,广告投放省心省力

带你认识40G单纤双向光模块-QSFP+ BiDi光模块

高效时代,电商运营如何靠RPA快速提效?

Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’

SAP 云平台上一种 Low Code Development(低代码开发)解决方案

干货|如何在海量文件系统中选择合适自己的文件系统

用汇编实现爱心特效【七夕来袭】

LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
随机推荐
The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"
从零开始入门单片机(一):必会背景知识总结
软件测试X模型
牛客网项目17节生成验证码 刷新验证码一直没反应
R语言使用zoo包中的rollapply函数以滚动的方式、窗口移动的方式将指定函数应用于时间序列、设置align参数指定结果数据中的时间标签取自窗口中的位置(参数right指定取自窗口的最右侧)
记某社区问答
QT专题:组合会话框和文本编辑器
QT专题:事件机制event基础篇
二维数组零碎知识梳理
剑指offer专项突击版第17天
QT专题:自定义部件
currentstyle 织梦_dede currentstyle属性完美解决方案
leetcode 62. Unique Paths(独特的路径)
Getting Started with SCM from Scratch (1): Summary of Background Knowledge
Naive Bayesian Method of Li Hang's "Statistical Learning Methods" Notes
iNFTnews | 看见元宇宙的两面,何谓全真互联网和价值互联网?
function call to print lua internal structure
Facebook's automated data analysis solution saves worry and effort in advertising
百战RHCE(第四十六战:运维工程师必会技-Ansible学习1-基础知识讲解)
身为程序猿——谷歌浏览器的这些骚操作你真的废吗!【熬夜整理&建议收藏】[通俗易懂]