PROGRAM :–1
class array;
rand byte unsigned arr[];
constraint c_arr{
arr.size() == 5;
arr.sum() == 60; //inside {[1:1000]}; // if you want a positive result
//foreach(arr[i]) arr[i] inside {[0:500]};
}
endclass
module tb;
array a=new();
int sum;
initial
repeat(10)
begin
a.randomize();
sum=0;
foreach(a.arr[i])
sum=sum+a.arr[i];
$display("sum =%0d elements of the array to sum up to <1000 ---->> %0p",sum,a.arr);
end
endmodule
RESULT :–
sum =60 elements of the array to sum up to <1000 ---->> '{'h28, 'hb, 'h9, 'h0, 'h0}
sum =60 elements of the array to sum up to <1000 ---->> '{'h2, 'h0, 'h7, 'h0, 'h33}
sum =60 elements of the array to sum up to <1000 ---->> '{'h4, 'h0, 'h0, 'h35, 'h3}
sum =60 elements of the array to sum up to <1000 ---->> '{'hb, 'h10, 'h19, 'h8, 'h0}
sum =60 elements of the array to sum up to <1000 ---->> '{'h0, 'h37, 'h5, 'h0, 'h0}
sum =60 elements of the array to sum up to <1000 ---->> '{'h3, 'h0, 'h0, 'h38, 'h1}
sum =60 elements of the array to sum up to <1000 ---->> '{'h0, 'h5, 'h15, 'ha, 'h18}
sum =60 elements of the array to sum up to <1000 ---->> '{'ha, 'h0, 'h0, 'h0, 'h32}
sum =60 elements of the array to sum up to <1000 ---->> '{'h1a, 'h5, 'h2, 'h1b, 'h0}
sum =60 elements of the array to sum up to <1000 ---->> '{'h10, 'h1b, 'h11, 'h0, 'h0}
PROGRAM :–2
class array;
rand int unsigned arr[];
constraint c_arr{
arr.size() == 5;
arr.sum() == 60; //inside {[1:1000]}; // if you want a positive result
//foreach(arr[i]) arr[i] inside {[0:500]};
}
endclass
module tb;
array a=new();
int sum;
initial
repeat(10)
begin
a.randomize();
sum=0;
foreach(a.arr[i])
sum=sum+a.arr[i];
$display("sum =%0d elements of the array to sum up to <1000 ---->> %0p",sum,a.arr);
end
endmodule
RESULT:–
sum =60 elements of the array to sum up to <1000 ---->> '{'h4958832, 'h8ae8ecd3, 'hc4b9c064, 'h988848e0, 'h133f81f3}
sum =60 elements of the array to sum up to <1000 ---->> '{'h8e4ecd87, 'h4998543e, 'hb87f7c8b, 'h14cfff46, 'h5ac962a6}
sum =60 elements of the array to sum up to <1000 ---->> '{'he004869d, 'h390dda9c, 'h3670936e, 'hfbdf72e4, 'hb49d98b1}
sum =60 elements of the array to sum up to <1000 ---->> '{'h17d4fe53, 'h36b21455, 'hba7f13cd, 'h1d043a6a, 'hd9f59f5d}
sum =60 elements of the array to sum up to <1000 ---->> '{'h10d61bea, 'hd66d3faf, 'h23fe333c, 'h1f2447d9, 'hd59a298e}
sum =60 elements of the array to sum up to <1000 ---->> '{'h18028825, 'h8fb317cd, 'h7f98477f, 'hb7b3d42b, 'h20fe44a0}
sum =60 elements of the array to sum up to <1000 ---->> '{'hb6e94ce8, 'h3190c552, 'h750017d2, 'h59342392, 'h4951b29e}
sum =60 elements of the array to sum up to <1000 ---->> '{'hf0364918, 'hcd2504ea, 'hb7e8329a, 'h443f065b, 'h467d7945}
sum =60 elements of the array to sum up to <1000 ---->> '{'h8f09a442, 'h94eb9b2, 'h93c8b596, 'heff84f0e, 'he3e69da4}
sum =60 elements of the array to sum up to <1000 ---->> '{'h54d814c5, 'hb729f759, 'ha2c604ba, 'h52b1c34b, 'hfe862c19}
PROGRAM :–3
class array;
rand int unsigned arr[];
constraint c_arr{
arr.size() == 5;
arr.sum() < 60; //inside {[1:1000]}; // if you want a positive result
//foreach(arr[i]) arr[i] inside {[0:500]};
}
endclass
module tb;
array a=new();
int sum;
initial
repeat(10)
begin
a.randomize();
sum=0;
foreach(a.arr[i])
sum=sum+a.arr[i];
$display("sum =%0d elements of the array to sum up to <1000 ---->> %0p",sum,a.arr);
end
endmodule
RESULT:–
sum =54 elements of the array to sum up to <1000 ---->> '{'h0, 'h0, 'h0, 'h0, 'h36}
sum =59 elements of the array to sum up to <1000 ---->> '{'h0, 'h2b, 'h10, 'h0, 'h0}
sum =35 elements of the array to sum up to <1000 ---->> '{'h0, 'h0, 'h23, 'h0, 'h0}
sum =42 elements of the array to sum up to <1000 ---->> '{'h4, 'h26, 'h0, 'h0, 'h0}
sum =43 elements of the array to sum up to <1000 ---->> '{'h0, 'h2b, 'h0, 'h0, 'h0}
sum =38 elements of the array to sum up to <1000 ---->> '{'h0, 'h0, 'h0, 'h24, 'h2}
sum =38 elements of the array to sum up to <1000 ---->> '{'h19, 'h0, 'h7, 'h0, 'h6}
sum =18 elements of the array to sum up to <1000 ---->> '{'h8, 'h0, 'h0, 'h0, 'ha}
sum =34 elements of the array to sum up to <1000 ---->> '{'h13, 'h0, 'he, 'h1, 'h0}
sum =32 elements of the array to sum up to <1000 ---->> '{'h0, 'h0, 'h16, 'h0, 'ha}
Case 1:
For int : arr.sum() == 60; -->No overflow values are printed.
For int : arr.sum() < 60; -->Overflow values are printed.
Case 2:
Was expecting overflow for byte datatype also.