In reply to sk7799:
Your approach to learning Constrained Randomization reminds me of the Infinite Monkey Protocol. If you keep throwing arbitrary constraints at the problem, you’ll eventually get the correct result! It looks like you cut and pasted code snippets from different examples and tried putting them together into a single randomization.
You wanted to create an array of 200 integers, yet nowhere do you create an array of 200 integers. You cannot use a distribution constraint if you need an exact count. You need to use the sum() reduction method to count the number of occurances.
This is the brute force way of creating the constraints, but you probably won’t be able to run this with the tools on EDAPlayground as it creates way too many interconnected constraints.
class packet;
parameter int N = 15;
rand byte unsigned array[N];
rand byte unsigned list[5];
//array can have 5 random numbers in the range [0:50]
constraint c1 {foreach(list[i]) list[i] inside {[0:50]}; unique {list}; }
//constraint such that each element should appear exactly 40 times in the array with 200 elements
constraint c2 {
foreach(list[i]) array.sum() with (int'(item==list[i])) == N/5; }
//constraint such that no same number should appear in any 4 consecutive slots
constraint c4 {foreach(array[i]) i< N-3 ->
{array[i] == array[i+1] ->
array[i] == array[i+2] ->
array[i] != array[i+3];
}}
endclass
module top;
initial begin
//handle and constructor for the class
packet pkt;
pkt = new();
repeat(200) begin
assert(pkt.randomize());
foreach (pkt.array[i]) $display("array[%0d] = %d",i,pkt.array[i]);
$display;
end
end
endmodule
You might have to break it up into two separate randomizations:
class packet;
parameter int N = 200;
rand byte unsigned array[N];
byte unsigned list[5];
function void pre_randomize();
//array can have 5 random numbers in the range [0:50]
assert(std::randomize(list) with {
foreach(list[i]) list[i] inside {[0:50]}; unique {list}; });
endfunction
//constraint such that each element should appear exactly 40 times in the array with 200 elements
constraint c2 {
foreach(list[i]) array.sum() with (int'(item==list[i])) == N/5; }
//constraint such that no same number should appear in any 4 consecutive slots
constraint c4 {foreach(array[i]) i< N-3 ->
{array[i] == array[i+1] ->
array[i] == array[i+2] ->
array[i] != array[i+3];
}}
endclass
module top;
initial begin
//handle and constructor for the class
packet pkt;
pkt = new();
repeat(2) begin
assert(pkt.randomize());
foreach (pkt.array[i]) $display("array[%0d] = %d",i,pkt.array[i]);
$display;
end
end
endmodule