Interview question on constraint

In reply to burra thanuja:
Hi burra.,
Please refer below solution for ur doubt,


// Code your testbench here
// or browse Examples
module top;
  
  class test;
    
  rand bit[3:0]array[10];
  
  constraint cnst_array{
    foreach(array[i])
    {
      foreach(array[j])
      {
        if(i!=j)
        {
          array[i] != array[j];
        }
      }
    
    }
      }
  
function void post_randomize();
        //array[$urandom_range(0,9)]=array[$urandom_range(0,9)]; // below is alternate for 
                                                                 //this solution
      int i=$urandom_range(0,8);
      array[i]=array[i+1];  //though itseems everytime alternate index will be having same 
                            //values,that's fine as the main context is to have same value in 
                            //any two different indexes.
endfunction
          
  endclass    
          
  test t1;
   initial begin
     t1=new();
     t1.randomize();
     $display("array=%p",t1.array);
   end
endmodule

Result:=
array='{11, 6, 11, 9, 3, 13, 1, 8, 14, 5}

1 Like