class test_c;
int d_array[];
function my_rand();
std::randomize(d_array) with {
unique{d_array};
d_array.size() inside {[0:100]};
d_array.sum() inside {[0:100]};
foreach(d_array[i]) {
d_array[i] != i;
}
};
endfunction
endclass
module dut;
initial begin
test_c test;
test =new();
assert(test.randomize());
test.my_rand();
foreach(test.d_array[i]) begin
$display("d_array[%02d]=%02d", i, test.d_array[i]);
end
end
endmodule
To check the randomized elements in d_array, I implemented
foreach(test.d_array[i]) begin
$display(“d_array[%02d]=%02d”, i, test.d_array[i]);
end
But I array values are broken. What causes the failure of the randomization process?
Errors: 0, Warnings: 2
vsim dut -batch -do “vsim -voptargs=+acc=npr; run -all; exit” -voptargs=“+acc=npr”
Start time: 05:02:50 on Jun 19,2023
** Note: (vsim-3812) Design is being optimized…
** Warning: testbench.sv(9): (vopt-2240) Treating stand-alone use of function ‘randomize’ as an implicit VOID cast.
** Warning: testbench.sv(7): (vopt-2250) Function “my_rand” has no return value assignment.
** Warning: testbench.sv(34): (vopt-2240) Treating stand-alone use of function ‘my_rand’ as an implicit VOID cast.
** Note: (vsim-12126) Error and warning message counts have been restored: Errors=0, Warnings=3.
What makes you think that the randomize() call failed? You are using signed 32-bit integers. If you add all the numbers and account for overflow, you will get a value between 0 and 100.
If you want each number to be between 0 and 100, you should add another constraint, although it is impossible to get 100 unique integers between 0 and 100 to sum to a total between 0 and 100.
Could you give me an example for “If you add all the numbers and account for overflow,” I want each number to be between 0 and 100, but don’t know what I should add another constraint. What I need to add more for the constraint?
class test_c;
int d_array[];
function int my_rand();
return std::randomize(d_array) with {
unique{d_array};
d_array.size() inside {[0:100]};
d_array.sum() inside {[0:10000]};
foreach(d_array[i]) {
d_array[i] inside {[0:100]};
d_array[i] != i;
}
};
endfunction
function void print();
foreach(d_array[i])
$display("d_array[%02d]=%02d", i, d_array[i]);
endfunction
endclass
module testbench;
test_c test;
initial begin
test =new();
if (!test.my_rand()) $display("Randomization failed!");
else test.print();
end
endmodule