How to generate a random array with unique element while the sum of all elements are bounded?

Hi,

Let’s say I have the following class, I want to randomly fill the array with unique element and yet suffice the sum constraint.
Instead of using nested foreach constraint, as it will significantly slow down the simulation, is there any other way to do this?

class array_wrapper;
  rand bit[7:0] array[10];
  constraint c_sum{array.sum()<1024;};

P.S:
One method I can think about is to add a helper class with randc variable as we normally used in generating unique array and then redo the randomization if the sum exceeds 1024.

class help_c;
  randc bit[7:0] a;
endclass

class array_wrapper;
  rand bit[7:0] array[10];
  constraint c_sum{array.sum()<1024;};
  help_c helper;
  function void post_randomize();
    do begin
      helper = new();
      foreach(array[i])begin
        helper.randomize();
        array[i]=helper.a;
      end
    end 
    while(array.sum()<1024);
  endfunction
endclass

Just wondering if there is a more neat way to do this?

Thanks in advance,

Hao

In reply to peterjin:

There ia a unique constraint.

class array_wrapper;
  rand bit[7:0] array[10];
  constraint c_sum{ array.sum()<1024; };
  constraint c_uni( unique {array}; }
endclass