Consider the following Verilog code that uses a random stimulus generator inside an initial block. What will be the value of data after 5 time units?
module tb; reg clk = 0; reg [3:0] data; initial begin data = 4'b0000; #5; data = $urandom_range(0, 15); end always #1 clk = ~clk; endmodule
Think about what $urandom_range does inside an initial block in simulation.
The $urandom_range(0, 15) function generates a random integer between 0 and 15 inclusive. Since data is assigned this value after 5 time units, it will hold a random 4-bit value in that range.
Choose the correct statement about using randomized stimulus in Verilog testbenches.
Think about the purpose of random stimulus in testbenches.
Randomized stimulus is used in testbenches to automatically generate different input values to test various scenarios. It is not synthesizable and $urandom is preferred over $random for better randomness. The sequence can be different each run unless seeded.
Examine the following Verilog code snippet used in a testbench:
initial begin
integer seed = 12345;
for (int i = 0; i < 5; i++) begin
data[i] = $urandom(seed) % 16;
end
endWhy does this code produce the same random sequence every simulation run?
Consider how seeding affects pseudo-random number generators.
Passing a fixed seed to $urandom causes it to generate the same sequence of random numbers every time, which is useful for repeatable tests.
What is the syntax error in the following Verilog code snippet?
initial begin reg [3:0] data; data = $urandom_range(0, 15) end
Check the end of each statement carefully.
In Verilog, each statement must end with a semicolon. The assignment to data is missing a semicolon.
Given the following Verilog code snippet generating random 4-bit values:
initial begin
for (int i = 0; i < 10; i++) begin
data = $urandom_range(0, 15);
#1;
end
endHow many unique 4-bit values can data take during this loop?
Consider the range of $urandom_range(0, 15) and the bit width of data.
The function $urandom_range(0, 15) can produce any integer from 0 to 15 inclusive, which fits exactly in 4 bits. So data can take 16 unique values.
