Dear Everyone. Below is the problem statement and the code I have written. The question is why am I not getting exactly four 7s despite using weighted constraints? I have provided the results of two simulator tools which I have used in edaplayground. Also I would like to know if there is a better solution.
########################Synopsys VCS result ###################
q='{11, 18, 2, 7, 4, 9, 5, 20, 4, 7, 18, 4, 2, 7, 12}
V C S S i m u l a t i o n R e p o r t
######################## Siemens Questa result #################
run -all
q=7 4 1 20 7 2 2 7 15 7 0 7 17 7 1
###########################################################
Problem Statement:
Write constraint for the below requirements :
a)The queue size will be 15. Randomize a queue such that it exactly has four 7 in it.
b) No 7’s should be at the consecutive next to each other
// Code your testbench here
// or browse Examples
class prac;
rand int q[$];
//size constraint and also constrained queue's values for simplicity, though not mentioned in the question
constraint c1 {q.size() == 15;
foreach(q[i])
q[i] inside {[0:20]};}
//used weighted constraint 27% (i.e 4/15 values to be 7)
constraint c2 {foreach(q[i])
q[i] dist {7:/27, [0:6]:/36, [8:20]:/37 };}
//constraint for no consecutive 7's
constraint c3 {foreach(q[i])
if(q[i] == 7 && (i+1) <=14)
q[i + 1] != 7;}
endclass
module test;
prac p;
initial begin
p = new();
p.randomize();
$display("q=%0p",p.q);
end
endmodule
A dist constraint gives you a statistical approximation. As the number of randomizations increases, the distribution gradually converges to the specified weight. In your example, if you called randomize() many times, you would see that the average number of 7’s would approach 4. However, obtaining the exact distribution of the desired weight becomes increasingly difficult due to the exponential increase in possible outcomes.
Consider a simple example of flipping a coin heads or tails. With 2 coin flips, there are 4 possible outcomes, but only 2 have an exact split of 1 head and 1 tail, or a 50% chance of achieving this. Conversely, if the number of coin flips is increased to 10, the probability of obtaining exactly 5 heads and 5 tails drops to 25%.
If you need an exact count of values, use the array sum() reduction method instead
class prac;
rand int q[$];
//size constraint and also constrained queue's values for simplicity, though not mentioned in the question
constraint c1 {q.size() == 15;
foreach(q[i])
q[i] inside {[0:20]};}
// count of 7's must be exactly 4
constraint c2 {
q.sum() with (int'(item==7)) == 4;
}
//simpler constraint for no consecutive 7's
constraint c3 {foreach(q[i])
i>0 && q[i] == 7 -> q[i-1] != 7;
}
endclass
module test;
prac p = new;
initial repeat(5) begin
assert(p.randomize());
$display("q=%0p",p.q);
end
endmodule
1 Like
Thank you for your response. I would like to ask another question. Why do simulators display the same randomized values everytime I hit the run button? This is not vendor specific, every simulator tool behaves the same. Is this not biased?
This behavior is defined in section 18.14 Random stability of the SystemVerilog LRM. When a given seed is used, tools must generate the exact same sequence of random numbers during repeated simulations. This ensures that the exact same stimulus can be reproduced for debugging purposes. Tools have command-line switches that allow you to start with a specific or random seed.
1 Like