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

In reply to Akhil Mehta:

There are several ways. I believe the usage of unique keyword inside constraints would be pretty straightforward.


class test;
  rand bit [2:0] arr[];

constraint c1{

arr.size == 6; // For example

foreach(arr[i])
  arr[i] inside{[0:arr.size()-1]};
 //arr[i] < arr.size();

}
  
  constraint c2{unique{arr};}

function void post_randomize();
  //arr.shuffle();
  
  $display("%0p",arr);
endfunction 

  
  
endclass : test


module top;
  
  test t1;
  
  initial begin
    
    t1 = new;
    t1.randomize();
    
  end
  
endmodule : top

And yeah there are some mistakes in your code. Try to observe them too.