Array with random numbers from 0 to Size-1 (Without duplicates)

In reply to Shubhabrata:

With some minor adjustments, you can vary the array size when calling randomize:


class test;
  rand bit [2:0] arr[];
  
  constraint arr_size_c {
    soft arr.size == 6;
  };
  
  constraint arr_values_c {
    foreach(arr[i])
      arr[i] inside{[0:arr.size()-1]};
  };
 
  constraint arr_unique_c {
    unique{arr};
  }
 
  function void post_randomize();
    $display("%0p",arr);
  endfunction 
endclass : test

module top;
 
  test t1;
 
  initial begin
    t1 = new;
    for (int i=5; i<=8; i++) begin
      if (!t1.randomize() with {t1.arr.size == i;})
        $display("Randomize failed");
    end
  end
 
endmodule : top