Randomized test stimulus in Verilog - Time & Space Complexity
When we use randomized test stimulus in Verilog, we want to know how the time to generate tests grows as we ask for more random values.
We ask: How does the number of operations change when we increase the number of random tests?
Analyze the time complexity of the following code snippet.
module random_test;
integer i;
reg [7:0] data;
initial begin
for (i = 0; i < 100; i = i + 1) begin
data = $random % 256;
$display("Random data: %0d", data);
end
end
endmodule
This code generates 100 random 8-bit numbers and prints each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs 100 times, generating and printing a random number each time.
- How many times: Exactly 100 times, once per loop iteration.
As the number of random values requested grows, the total operations grow in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 random generations and prints |
| 100 | About 100 random generations and prints |
| 1000 | About 1000 random generations and prints |
Pattern observation: The work grows directly with the number of random values requested.
Time Complexity: O(n)
This means if you ask for twice as many random values, the time to run roughly doubles.
[X] Wrong: "Generating random numbers is instant and does not add to time as n grows."
[OK] Correct: Each random number generation and print takes time, so more numbers mean more total time.
Understanding how loops with random generation scale helps you explain testbench performance clearly and confidently.
"What if we replaced the for-loop with a while-loop that stops when a certain random condition is met? How would the time complexity change?"
Practice
Solution
Step 1: Understand randomized test stimulus
Randomized test stimulus means giving the design many different input values automatically.Step 2: Identify the purpose
This helps find bugs by testing many cases without writing each one manually.Final Answer:
To automatically test the design with many different input values -> Option BQuick Check:
Randomized testing = automatic varied inputs [OK]
- Thinking it sets fixed inputs manually
- Confusing with clock generation
- Assuming it speeds synthesis
Solution
Step 1: Recall Verilog random functions
$urandom_range(min, max) generates a random number between min and max inclusive.Step 2: Check options
Only $urandom_range(0, 15) uses $urandom_range with correct syntax and parameters.Final Answer:
$urandom_range(0, 15) -> Option DQuick Check:
$urandom_range(min, max) = correct syntax [OK]
- Using $random with parameters (wrong syntax)
- Using random without $ sign
- Using $urandom without range
reg [3:0] data;
initial begin
data = $urandom_range(0, 15);
$display("Data = %d", data);
endWhat is the expected output?
Solution
Step 1: Understand $urandom_range usage
$urandom_range(0, 15) returns a random integer from 0 to 15 inclusive.Step 2: Analyze $display output
The $display prints the value of data, which is the random number assigned.Final Answer:
Data = a random number between 0 and 15 -> Option AQuick Check:
Random assignment prints random value [OK]
- Assuming fixed value 0 or 15
- Expecting syntax error
- Confusing $urandom_range with $random
reg [7:0] input_signal; initial begin input_signal = $urandom_range(255); end
Solution
Step 1: Check $urandom_range usage
$urandom_range requires two arguments: minimum and maximum values.Step 2: Identify missing argument
Code uses only one argument (255), missing the minimum value.Final Answer:
Missing second argument in $urandom_range -> Option AQuick Check:
$urandom_range needs min and max [OK]
- Using only one argument in $urandom_range
- Thinking reg cannot be assigned in initial
- Confusing reg and wire usage
Solution
Step 1: Understand repeated random assignment every 10 time units
Using initial with forever loop and delay #10 repeats the assignment continuously.Step 2: Check options
The always #10 option causes syntax issues due to improper structure.
The @(posedge clk) option triggers on clock edges instead of time delays.
The simple initial option assigns only once.
The initial begin forever #10 option correctly repeats every 10 time units.Final Answer:
initial begin forever #10 input_signal = $urandom_range(3, 12); end -> Option CQuick Check:
Use initial + forever + #delay for repeated random stimulus [OK]
- Using always without begin-end block
- Assigning only once in initial
- Confusing clock edge with time delay
Ready to prove it?
Pass the test and this pattern moves unknown
