Unique values of randomization

Hi

I need to randomize a variable X . it needs to take from 1 to 16
Another variable Y , it needs to take from 1 to 16
I should get a combination of X and Y , such that
X =1 , Y = {1 to 16}
X =2 , Y = {1 to 16}
X =3, Y = {1 to 16}

.
X=16, Y = {1 to 16}
Here there 16X16 = 256 unique combinations.
How can i write a constraint like that .


rand bit [4:0] x , y ;
rand bit [9:0] xy ; // Concatenation of {x,y}
     bit [9:0] xy_q[$] ;

constraint VAL { x inside {[1:16]} ; y inside {[1:16]} ; }

constraint CONCAT { xy == { x , y } ; } 

constraint UNIQ {      foreach(xy_q[i])
                    unique { xy , xy_q[i] } : 
                 }

// Would have been simpler if below Concatenation Constraint worked !!
// constraint CONCAT_UNIQ { unique {  {x,y} } ; } 

function void post_randomize() ;
 
   xy_q.push_back( {x,y} ) ;
   
  if ( xy_q.size() == 256 ) 
      xy_q.delete() ;

endfunction

Keen for a cleaner alternate solution via less variables

In reply to ABD_91:

Hi Thanks for the reply
I didnot understood. Can you please write the complete code.

In reply to naaj_ila:

class A;

  rand bit [4:0] X , Y ;
  rand bit [9:0] XY ;
 
  bit [9:0] XYQ[$];
  constraint VAL {
    X inside {[1:16]};
    Y inside {[1:16]} ;
    {X,Y} == XY;
  }
  constraint XY_UNIQ { unique { XYQ, XY } ; } 
 
  function void post_randomize() ;
    XYQ.push_back( {X,Y} );
 
    if ( XYQ.size() == 256 )
      XYQ = {};
  endfunction
endclass
module top;
  A a= new;
  initial for(int i=1;i<270;i++) begin
    assert(a.randomize());
    $display("%3d X %d Y %d",i, a.X, a.Y);
  end
endmodule

In reply to dave_59:

Hi Dave, Thanks it worked.

  1. There are two varaible,X,Y
    Actaully there can be multipe varaible from 2 to 10 depends on one passing variable variable

  2. I just rearranged the code with queue
    In this below code, i passed num_var=2, it worked as it
    Say if num_var=3, how it will work?
    I am not getting how we can concatinate with XY which is 10 bits.



class A;
 
  //rand bit [4:0] X , Y ;
  rand bit [4:0] Var_q[$];
  int            num_var;
  rand bit [9:0] XY ;
 
  bit [9:0] XYQ[$];
  constraint VAL {
    Var_q.size() == num_var; 
    //X inside {[1:16]};
    //Y inside {[1:16]} ;
    {Var_q[0],Var_q[1]} == XY;
  }
  constraint XY_UNIQ { unique { XYQ, XY } ; } 
 
  function void post_randomize() ;
    bit [9:0] TMP;
    foreach(Var_q[i]) begin 
      TMP = {TMP,Var_q[i]};
    end
    //XYQ.push_back( {X,Y} );
 
    if ( XYQ.size() == 256 )
      XYQ = {};
  endfunction
endclass
module top;
  A a= new;

  initial begin 
    a.num_var = 2;
    for(int i=1;i<270;i++) begin
      assert(a.randomize());
      //$display("%3d X %d Y %d",i, a.X, a.Y);
      $display("%3d X %d Y %d",i, a.Var_q[0], a.Var_q[1]);
    end
  end
endmodule