Array constraint


In array 2 values should be same between index 0-7.Array elements should be between 1 to 9.

Result = '{'h0, 'h0, 'h0, 'h0, 'h0, 'h0, 'h0, 'h0, 'h0, 'h0}

Can anyone tell why it is not working?

Thanks !!!

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 };

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

In reply to UVM_geek:

There’s a contraint error; meaning that the constraints cannot be solved.

You have a array of size 10, you want them to be between 1 and 9, and unique.
You can only have 9 unique elements based on your above constraints.

An unsolved/error in constraint silver leaves the array as before, and hence the display with all elements in array as 0.

Thank you KillSteal for your response.

Hi Dave,
My expected output was {1,2,3,3,4,5,6,7,8,9}or {1,2,3,4,5,5,6,7,8,9}or …
I mean array size is 10 but it should has unique elements between 1 to 9.As I want 1 repeated set then it should satisfy right?
Please correct me if I am wrong.
“Also, constraints must be satisfied before calling post_randomize.”
May be sue to this it got failed as I am making duplicates in the post_randomize().

Thank you Dave for your response.