Array constraint

In reply to UVM_geek:

Your results are all zeros because the randomization failed. You should always test the result of the randomize function.

Your constraints fail because they have a conflict in that an array cannot have 10 unique values between 1-9. Also, constraints must be satisfied before calling post_randomize.

Here is a different approach:

class packet;
  rand bit [3:0] array[10], helper[9];
constraint c1 {
  foreach (array[i]) array[i] inside {[1:9]};
  foreach (helper[i]) helper[i] inside {array};
}
constraint unique_elements {
  unique { helper };
}
 
endclass

module test;
  packet p; 
  initial begin
    p=new();
    repeat (10) begin
      assert( p.randomize );
      $display("Result = %p ",p.array); //p.print();
    end
  end
endmodule