verilog 随机数生成module test;reg [31:0] a,b,c,d;initialbegina = $random % 100; a[31] = 0;b = $random % 100; b[31] = 0;c = $random % 100; c[31] = 0;d = $random % 100; d[31] = 0;endinitialbegin#10 $display("%d,%d,%d,%d",a,b,c,d);#1000 $finish;end
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/16 06:51:29
verilog 随机数生成module test;reg [31:0] a,b,c,d;initialbegina = $random % 100; a[31] = 0;b = $random % 100; b[31] = 0;c = $random % 100; c[31] = 0;d = $random % 100; d[31] = 0;endinitialbegin#10 $display("%d,%d,%d,%d",a,b,c,d);#1000 $finish;end
verilog 随机数生成
module test;
reg [31:0] a,b,c,d;
initial
begin
a = $random % 100; a[31] = 0;
b = $random % 100; b[31] = 0;
c = $random % 100; c[31] = 0;
d = $random % 100; d[31] = 0;
end
initial
begin
#10 $display("%d,%d,%d,%d",a,b,c,d);
#1000 $finish;
end
endmodule
这段代码在仿真时输出为
48,2147483549,2147483609,2147483639
为什么只有a是正确的?
verilog 随机数生成module test;reg [31:0] a,b,c,d;initialbegina = $random % 100; a[31] = 0;b = $random % 100; b[31] = 0;c = $random % 100; c[31] = 0;d = $random % 100; d[31] = 0;endinitialbegin#10 $display("%d,%d,%d,%d",a,b,c,d);#1000 $finish;end
{$random}%100.试试,random能生成负数.
若是-69,则-69%100=-69,补码的话看上去就会是一个很大的数.即便后来高位置零,其他位还是保留补码时的值,所以很大.