Generating an array of given size with unique values

Hello,
I have written a code to generate an array of unique values and of given size. The code is given below :


class key_rand;
  rand logic[255:0] key;
endclass

class randcrange;
  randc logic [15:0] rvalue;
  **logic [15:0] max_value;**
  function new (max_value);
    this.max_value = max_value;
  endfunction
 **constraint mx_cal_con {rvalue < max_value;};**
endclass

class unq_arr_cl;
  logic [15:0] max_ar_size;
  logic [15:0] max_val;
  rand logic [15:0] a[];
  constraint a_size {a.size == max_ar_size;};
  function new( logic [15:0] max_ar_size, logic [15:0] max_val =  1);
    if (max_val <= max_ar_size) begin
      max_val = max_ar_size;
    end
    this.max_val = max_val;
    this.max_ar_size = max_ar_size;
  endfunction
  function void post_randomize;
    randcrange rr;
    rr = new (max_val);
    foreach (a[i]) begin
      **assert (rr.randomize(**))
      else $display ("here randomize failed");
      a[i] = rr.rvalue;
    end
  endfunction
  function void display_values();
    $display ("here");
    $display ("value of a.size is %b", a.size());
    $display ("value of max_ar_size is %b", max_ar_size);
    for (int i = 0; i <= a.size-1; i++) begin
      $display ("value of i = %d, a[i]= %b", i, a[i]);
    end
  endfunction
endclass

program test;
  unq_arr_cl unq_arr_clinst;
  initial begin
    unq_arr_clinst = new(4);
    assert (unq_arr_clinst.randomize())
    else $fatal(0, "randomization failed");
    unq_arr_clinst.display_values(); 
  end;
endprogram

code compiles however, after running the code, I am getting following error message :
"testbench.sv, 31
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.

“testbench.sv”, 31: $unit::\unq_arr_cl::post_randomize .unnamed$$_0.unnamed$$_1: started at 0ns failed at 0ns
Offending ‘rr.randomize()’
here randomize failed
here
value of a.size is 00000000000000000000000000000100
value of max_ar_size is 0000000000000100
value of i = 0, a[i]= xxxxxxxxxxxxxxxx
value of i = 1, a[i]= xxxxxxxxxxxxxxxx
value of i = 2, a[i]= xxxxxxxxxxxxxxxx
value of i = 3, a[i]= xxxxxxxxxxxxxxxx
$finish at simulation time 0
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.520 seconds; Data structure size: 0.0Mb
Mon Apr 20 12:19:59 2020
Done
"
It seems the constraint rr.randomize() (given in bold above) is failing. If I check the value of max_value member in randcrange class, it is 0 while I expect it to be 4. So the constraint mx_cal_con {rvalue < max_value;} fails. If I remove this constraint, it works properly. Can someone help me to understand the problem please.

Thanks,
-sunil

In reply to puranik.sunil@tcs.com:
you used the logic that is 4 state variable. whats purpose of using logic as rand ? you should use bit intead of logic.

In reply to puranik.sunil@tcs.com:

The issue has to do with the namespace conflicts in the new() function of unq_arr_cl. The assignment of max_val ends up being 0, resulting in conflicting constraints.

You should change the parameters of your new() function to not conflict with local class variables so that it is clear what your are trying to accomplish.

Also, the new() function in randcrange doesn’t have a type associated with max_value. Without a type, it defaults to a single bit, resulting in an invalid passing of the parameter.