Generate unique elements in an array

In reply to dave_59:

In reply to mseyunni:

class A;
bit [PDW_INDEX_BITS-1:0] used_dcd2pdw[$][2**DCD_INDEX_BITS];
rand bit[PDW_INDEX_BITS-1:0] dcd2pdw[2**DCD_INDEX_BITS];
constraint uniq {unique {dcd2pdw,used_dcd2pdw};}
function void post_randomize;
used_dcd2pdw.push_back(dcd2pdw);
endfunction
endclass:A

The uniq constraint will eventually fail after so many calls to randomize(). I’ll leave it to you to figure out what to do when it does.

Hi dave,

I tried to use that but a compile error says that the other array is not rand.
This is my code:


program try_unique_array();

class unique_array;
  rand bit [3:0] arr[16];
       bit [3:0] excludes[4] = {0,5,4,3};

  constraint array_elements_should_be_unique {
    unique {arr, excludes};
  }
endclass : unique_array

unique_array ua;

initial begin
  ua = new();

  assert(ua.randomize());

  foreach(ua.arr[i]) begin
    $display("ua.arr[%0d] = %0d", i, ua.arr[i]);
  end
end

endprogram : try_unique_array

Regards,
Reuben