Achieving Unique Array via inside operator

Hi all ,

I was trying if I could achieve a unique array without array.sum() / unique constraint .

So I tried the following ::


module  UNIQ_via_inside ;
  
  class   Main ;
    
    rand  bit [2:0] b [] ;
    
    constraint  SIZE {  b.size()  ==  5 ;  }
    
    constraint  UNIQ  { 
       
                          foreach( b[ i ] ) 
                         {  
                           if(  i == 0 )
                           {  
                             !( b[i]  inside { b[ ( i+1) : ( b.size() - 1 ) ]  }  ) ; 
                           }
                             
                             else if ( i == ( b.size() - 1 ) )
                            {
                             
                              !( b[i]  inside { b[ 0: ( b.size() - 2 )] }  ) ;
                            }  
                             
                             else
                           {    
                             !( b[i]  inside { b[0 : ( i - 1 ) ] , b[ ( i + 1 ) : ( b.size() - 1 ) ]  }  ) ;                                          
                           }
    
                         }
                               
                     }          
    
    
  endclass  
  
  
  Main  m1  ;
  
  
  initial  begin
    
    m1 = new() ;
    
    repeat( 5 )
     begin
       
       if( m1.randomize() )    
         $display("Success  with  %0p " , m1.b);
       else
         $display(" FAILS ");
     end   
      
    
  end  
  
endmodule  

This however doesn’t work due to Illegal Slice Compilation Error

Any suggestions ?

In reply to Have_A_Doubt:

constraint  UNIQ  { 
    foreach( b[ i ] ) 
    foreach( b[ j ] ) {
if (i != j) b[i] != b[j];
}
}

In reply to Jianfeng_0:

My apologies , I forgot to mention that I was trying to achieve unique without the above dual foreach logic too .

In reply to Have_A_Doubt:

https://verificationacademy.com/forums/systemverilog/unique-array-elements-without-rand-or-randc

In reply to dave_59:

Dave ,
Need suggestions with the following logic ::


rand  bit [2:0] b [] ;

rand  bit [2:0] bhelper[][] ;

      bit [2:0]  N  = 5 ;  //  Can  be  changed  at  Run - Time .
 
    constraint  SIZE   {  b.size()  ==  N  ;  }

    constraint  SIZE1  {  bhelper.size() ==  b.size() ; } 

    constraint  SIZE2  {    foreach( bhelper[ i ] ) 
                          {
                             bhelper[i].size() == ( b.size() - 1 ) ;
                          }
                        }
 

    constraint  VAL     {     foreach( b[ i ] )
                             { 
                                ! ( b[ i ]  inside { bhelper[ i ] } )  ; 
                             } 
                         }

     //  Need  Logic  here  to  constraint  elements  of  bhelper  !! 

The constraint logic should be Generic . For N == 5 it would be as follows ::



 //  b[0]  Should  Not  Occur  within  bhelper[0]  ::

 bhelper[0][0]  == b[1] ;  bhelper[0][1]  == b[2] ;  
 bhelper[0][2]  == b[3] ;  bhelper[0][3]  == b[4] ;

 //  b[1]  Should  Not  Occur  within  bhelper[1]  ::

 bhelper[1][0]  == b[0] ;  bhelper[1][1]  == b[2] ;  
 bhelper[1][2]  == b[3] ;  bhelper[1][3]  == b[4] ;

//  b[2]  Should  Not  Occur  within  bhelper[2]  ::

 bhelper[2][0]  == b[0] ;  bhelper[2][1]  == b[1] ;  
 bhelper[2][2]  == b[3] ;  bhelper[2][3]  == b[4] ;

//  b[3]  Should  Not  Occur  within  bhelper[3]  ::

 bhelper[3][0]  == b[0] ;  bhelper[3][1]  == b[1] ;  
 bhelper[3][2]  == b[2] ;  bhelper[3][3]  == b[4] ;

//  b[4]  Should  Not  Occur  within  bhelper[4]  ::

 bhelper[4][0]  == b[0] ;  bhelper[4][1]  == b[1] ;  
 bhelper[4][2]  == b[2] ;  bhelper[4][3]  == b[3] ;

 
 

In reply to Have_A_Doubt:

I cannot see how you could build bhelper without two nested foreach loops.

Given that this a very contrived coding question for you to solve with an impractical solution, I will not do your work for you.