Help on distriution constraint for a variable to have a value within array values, with weights in another array

Hi I am getting the conflicts for the below code. how to write the code with mentioning the weights in an array and values in another array and how to associate values and weights


   
module newone;

class sri;
   rand int portmode;
   int a[3]={0,1,2};
   int c[3]={100,100,100};
   constraint c_dist {
      foreach (a[i])
      portmode dist {a[i]:=c[i]};
   }
endclass

sri sri_i;

initial begin
  sri_i=new();
  for(int i=0;i<4;i++)begin
    sri_i.randomize();
    $display("portmode %d",sri_i.portmode);
  end
end
endmodule

  


  

In reply to srbeeram:
You can not have multiple dist constraints on the same random variable with different sets of values - only one of them could ever evaluate true. You need to unroll the foreach loop into a single dist constraint.

      portmode dist {a[0]:=c[0],a[1]:=c[1],a[2]:=c[2]};

If the size of the array is not fixed, then you need a completely different approach. You will need to pick a random value between 1 and c.sum() (or 0 and c.sum()-1) and define a function that uses that value to select a value for an index into a. For example 0-99 selects a[0], 100-199 selects a[1], and 200-299 selects a[3]

function int lookup(int value);
  int limit=0;
  foreach (c[ii]) if (value < (limit += c[ii])) return a[ii];
  // error if you reach here
endfunction

In reply to dave_59:

Thanks dave for the clarifications. This code I need to write multiple times with different weights and size of a is not small. so checking whether it can be simplified.

In reply to srbeeram:
You could certainly generalize this function to be used in a number of distrbutions.

function int lookup_dist(int weights[]); // input is array of dist weights
                                    // returns random index
  int value = $urandom_range(weights.sum()-1);
  int limit=0;
  foreach (weights[ii]) if (value < (limit += weights[ii])) return ii;
  // error if you reach here
endfunction

...
portmode == a[lookup_dist[c]];


In reply to dave_59:

Thanks a lot Dave for the help.