Hello there!
I am trying to generate 10 values which are divisible by 5 in its ascending order. I am unable to understand where I am doing it wrong. Here is the code -
class random_value;
rand bit [7:0]var1 ;
rand int i ;
Hello chetansahu, thanks for your code. I tried this code, and it says “index out of bounds (values[(9 + 1)]) for unpacked array of packed elements in constraint”.
class random_values;
rand int unsigned values[];
rand int unsigned value_size;
constraint value_size_c { soft value_size == 10; } // Array size. Soft to allow override in randomize() call
constraint size_c { values.size() == value_size; }; // Set values[] size from value_size
constraint value_c { foreach (values[i]) values[i] < values.size() * 2;} // Constrain to max of (2 * size of array)
constraint unique_c { unique {values}; }; // Each value is unique
function void post_randomize();
foreach (values[i]) values[i] = values[i] * 5; // Make each value divisible by 5
values.sort(); // Make values ascending
endfunction
endclass
module test;
random_values rv = new();
initial begin
if (!rv.randomize())
$display("Randomization failed!");
else
$display("Random values: %p", rv.values);
if (!rv.randomize() with { value_size == 20;}) // Make a bigger array
$display("Randomization failed!");
else
$display("Random values: %p", rv.values);
end
endmodule