write a Constraint to generate 5 digit number out of which first two digits is multiple of 7 and last 3 digits could be any thing from 0 to 5
In reply to CJRS:
Hi, I hope the following code becomes helpful to you.
class tr;
rand byte unsigned array[5];
constraint condition{
foreach(array[i])
{
(i==0) || (i==1) -> (array[i]>0) && (array[i] % 7 == 0);
(i>1) -> (array[i] inside {[0:5]});
}
}
endclass
module test;
tr t=new;
initial begin
assert(t.randomize)
$display("%0p",t.array);
end
endmodule
Hi Shubhabrata,
The output of above code is :
'{'hee, 'h5b, 'h2, 'h2, 'h2}
'{'h8c, 'hf5, 'h2, 'h5, 'h1}
'{'h38, 'haf, 'h2, 'h4, 'h3}
'{'h38, 'h2a, 'h2, 'h3, 'h5}
'{'hb6, 'h15, 'h2, 'h0, 'h5}
Here sometimes the last 3 digits have repeated values but it should be unique.(Ex 1st one).
Please Correct me if I am wrong.
Thanks !!!
In reply to UVM_geek:
Hi,
I guess that doesn’t matter because the question doesn’t contain any unique elements points. If you want you can add the unique keyword in the constraint .
class tr;
rand byte unsigned array[5];
constraint condition{
foreach(array[i])
{
(i==0) || (i==1) -> (array[i]>0) && (array[i] % 7 == 0);
(i>1) -> (array[i] inside {[0:5]});
}
}
constraint condition2{unique{array};}
endclass
module test;
tr t=new;
initial begin
repeat(5) begin
assert(t.randomize)
$display("%0p",t.array);
end
end
endmodule
Thank you Shubhabrata .