What is missing makes constraints results incorrect?

I want uniq/ascending values until the tmp_num and rest of the values from the idx = 0 to tmp_num-1.


   rand bit [7:0] uniq_dup_fol[];
    rand bit [4:0] tmp_num;
    constraint uniq_dup_c {
        solve tmp_num before  uniq_dup_fol;
        uniq_dup_fol.size() == 16;
        tmp_num inside {[uniq_dup_fol.size() - 6 : uniq_dup_fol.size() - 4]};
        foreach (uniq_dup_fol[idx]) {
            if(idx < tmp_num) {
                if(idx < tmp_num - 1) {
                    uniq_dup_fol[idx] < uniq_dup_fol[idx + 1];
                } else {
                    uniq_dup_fol[idx] > uniq_dup_fol[idx - 1];
                }
            } else {
                uniq_dup_fol[idx]  == get_num(tmp_num);
            }
        }
    }

    function bit[7:0]get_num(bit [4:0] tmp_num_l);
        bit [4:0] cnt;
        cnt = $urandom_range(1,tmp_num_l-1);
        get_num = uniq_dup_fol[cnt];
        $display("cnt = %0d uniq_dup_fol = %0h", cnt, uniq_dup_fol[cnt]);
        return uniq_dup_fol[cnt];
    endfunction : get_num

Results:

cnt = 5 uniq_dup_fol = 85
UVM_INFO sys_verif/verif/sys_trans_c.sv(76) @ 0: reporter [TB] -----------------------------------------
Name            Type          Size  Value
-----------------------------------------
inst            sys_trans_c   -     @2537
  tmp_num       integral      5     'ha  
  uniq_dup_fol  da(integral)  16    -    
    [0]         integral      8     'h3d 
    [1]         integral      8     'h3f 
    [2]         integral      8     'h44 
    [3]         integral      8     'h51 
    [4]         integral      8     'h54 
    ...         ...           ...   ...  
    [11]        integral      8     'h85 
    [12]        integral      8     'h85 
    [13]        integral      8     'h85 
    [14]        integral      8     'h85 
    [15]        integral      8     'h85 
  rst           integral      1     'h1  
  load          integral      1     'h1  
  str           integral      1     'h1  
  data          integral      32    'h207
  o_data        integral      32    'h0  
  af            integral      1     'h0  
  ae            integral      1     'h0  
  full          integral      1     'h0  
  empty         integral      1     'h0  
-----------------------------------------

UVM_INFO sys_verif/verif/sys_trans_c.sv(77) @ 0: reporter [TB] '{'h3d, 'h3f, 'h44, 'h51, 'h54, 'h56, 'h5b, 'h5c, 'h7a, 'ha8, 'h85, 'h85, 'h85, 'h85, 'h85, 'h85}

What is missing is a complete testcase and a clear description of what results you are expecting. What do you want for values in the array above the index tmp_num?

module top;
class example;
 rand bit [7:0] uniq_dup_fol[];
 rand bit [4:0] tmp_num;
 constraint uniq_dup_c {
   uniq_dup_fol.size() == 16;
   tmp_num inside {[uniq_dup_fol.size() - 6 : uniq_dup_fol.size() - 4]};
   unique {uniq_dup_fol};
   foreach(uniq_dup_fol[i])
     (i >= 1 && i <= tmp_num) -> uniq_dup_fol[i-1] < uniq_dup_fol[i];
 }
endclass
 
 example ex = new;
 initial repeat(10) begin
   assert(ex.randomize());
   $display("tmp_num:%2d uniq_dup_fol: %0p",ex.tmp_num, ex.uniq_dup_fol);
 end
endmodule

I am trying to write a constraint such that the array contains the unique values upto the idx = tmp_num and rest of the values are duplicates from the list of 0:tmp_val-1

OK, that description is a bit clearer. I would use a temporary array list as a helper.

module top;
class example;
  rand bit [7:0] uniq_dup_fol[], list[];
  rand bit [4:0] tmp_num;
 constraint uniq_dup_c {
   uniq_dup_fol.size() == 16;
   tmp_num inside {[uniq_dup_fol.size() - 6 : uniq_dup_fol.size() - 4]};
   list.size == tmp_num;
   foreach(list[i]){
     (i > 0) -> list[i-1] < list[i];
     list[i] == uniq_dup_fol[i]; // uniq array from [0:tmp_num-1]
   }
   foreach(uniq_dup_fol[i])
     (i >= tmp_num) -> uniq_dup_fol[i] inside {list}; // remainder of array
 }
endclass
 
 example ex = new;
 initial repeat(10) begin
   assert(ex.randomize());
   $display("tmp_num:%2d uniq_dup_fol: %0p",ex.tmp_num, ex.uniq_dup_fol);
 end
endmodule

I really appreciate and admire your expertise. thank you for helping everyone.

I got this answer other way. I was just curios what was missing or wrong in the logic I posted earlier.

Anyway, We got the answer that is the objective.

thank you so much.

Using $urandom in a constraint is probably not going to work. Also, calling user defined functions in a constraint need careful consideration. See section 18.5.11 Functions in constraints in the 1800-2017 LRM.