Constraints


class packet;

rand bit [5:0] array [];
randc int arr_size;  

rand int i;
rand int prev_i;

  constraint size_array {array.size() <10;
 array.size==arr_size;}
constraint elements {

foreach (array[i])

{

array[i] inside {[0:64]}

};

}

constraint unique_elements {unique {array[i]};} //to make sure no elements repeat at an instance of randomization

constraint inequality {array.size > 3;}

function void post_randomize();

//prev_i = array.size;

endfunction

endclass: packet

module foreach_constraint;

initial

begin

packet pkt = new;

repeat (5)

begin

pkt.randomize();

$display("\nThe size of the array is %0d",pkt.array.size());

$display("elements of the array = %0p",pkt.array);

end

end

endmodule