I need to randomize a variable with equal distribution between {1 , [2:5], [6:15]}
ie, probability to generate 1 is 1/3
probability to generate 2…5 is 1/3
probability to generate 6…15 is 1/3
How do i define this in sv?
Below is the code i tried, but the distribution were not same for different seeds.
class constraint_c;
rand bit[3:0] ack_delay;
constraint ack_delay_constr { ack_delay dist {1 := 1, [2:5] :/ 1, [6:15] :/ 1}; }
function void post_randomize();
$display("ack_delay %d", ack_delay);
endfunction
endclass
module test_constraint();
constraint_c cl;
initial
begin
cl = new();
repeat (6)
begin
cl.randomize();
end
$finish();
end
endmodule
For getting stable distribution you have to use the same seed. And the distribution will be almost same in the long repeats, but not for the small repeats. For different seeds the random distribution order will be different, but for the same seed distribution order will be same.
The original example was modeled correctly. :/ applies the weight to the range as a whole. The constraint that Sasi wrote would give a uniform distribution to each value in the range.
For a large enough number of randomization calls, it should not matter if the seed is the same or different. How many calls did you make?
The program was with 6 iterations. If i increase the samples, weighted distribution works better.
The example posted can be considered as a testcase, that is required to cover 3 sets.
So i defined the testcase to randomize 2*(number of sets)times.
If i consider randc,
randc bit [2:0] cnt;
“cnt” is guaranteed to have different randomized value, and 1 testcase with 8 iterations of randomization is going to cover all the possibilities.
Using weighted distribution, How do we come up with a similar figure?
Apparently, you slept through your Probabilities and Statistics class, and you don’t gamble. Try flipping a coin 20 times and see how often it lands on the same side consecutively. A distribution is not like a hard constraint; it is only after a large number of iterations that your solution approaches the desired distribution.
If you have a hard constraint that a number from each of the three sets it picked in a cycle, then you cannot use the dist constraint. You can use the cyclic randc constraint to select between each set:
This gives you an exact even selection of each set in one cycle. If want multiple selection of sets in a cycle, or don’t want the selections to be even increase the delay_set_limit: