Interview question on constraint

In reply to burra thanuja:

There are many ways to do this, one way is to use the post randomization.

module abc;
  
  class aa;
    rand int unsigned val[10];
 
    constraint s1 {
      foreach(val[i]){
        val[i] < 10;
        foreach(val[j]){
          if(i < 9 && j < 9 && i != j)
            val[i] != val[j];
        }
      }
    }
    
     function void post_randomize();
        val[10] = val[$urandom_range(0,8)];
     endfunction
          
  endclass

 initial begin
    aa a1 = new();
    a1.randomize();
    $display(a1.val);
  end

endmodule
2 Likes

In reply to yourcheers:

Thanks you sir for helping with the answer.

In reply to burra thanuja:

Can anyone plz help me with this constraint question…
Take a rand variable with array size 10,need to get unique values in each location without using unique keyword and for any of 2 locations we need to get same value?


module abc;
  class aa;
    rand int unsigned val[10];
    rand int unsigned r_index1,r_index2;

    constraint s1 {
      r_index1 == inside {[0:9]};
      r_index2 == inside {[0:9]};
      r_index1 != r_index2;

      foreach(val[i]){ val[i] < 10; }

      foreach(val[i]){
        foreach(val[j]){
          if(i != j && i!=r_index1  && j!=r_index1)
            val[i] != val[j];
        }
      }

      val[r_index1] == val[r_index2];
    }	
  endclass
 
  initial begin
    aa a1 = new();
    a1.randomize();
    $display(a1.r_index1, a1.r_index2);
    $display(a1.val);
  end
endmodule

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

In reply to Yash_wanth12345:

In reply to burra thanuja:
function void post_randomize();
array[$urandom_range(0,9)]=array[$urandom_range(0,9)];
endfunction

$urandom_range(0,9) will return any number between 0 to 9, so there is no guarantee that you will always get different index values in LHS and RHS.

In reply to Alokpati:

HI,
Yes you are right.,
but we can do like below.,

function void post_randomize();
int i=$urandom_range(0,8);
array[i]=array[i+1];
endfunction

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.

In reply to Yash_wanth12345:
in the post randomize both the indexes of lhs and rhs might randomize to be the same value and it won’t meet the criteria ‘2 values should be same’
eg: a[2]=a[2] can happen

In reply to sriram_madavswamy:

Hi @sriram.,
Please refer to the updated solution know.


module tb;
  
  class myClass;
    rand int a[10];
    constraint c_a_val {
      foreach(a[i]) {
        a[i] inside {[1:10]}; 
        
        if(i%2 == 0) {
          a[i+1] == a[i];
          foreach(a[j]) {
            if( (j%2 == 0) && (i != j) ) {
              a[i] != a[j];
            }
          }
        }
      }
    }
    
    function void post_randomize();
      $display("a = %p", a);
    endfunction 
  endclass 
  

  
  initial begin 
    myClass obj = new();
    obj.randomize();
  end 

Probably, this should meet the query…

program tb;
class cls;
rand bit [3:0] arr[10], indx1, indx2;
constraint cons{
foreach(arr[i])
foreach(arr[j])
(i != j) → soft arr[i] != arr[j];

        indx1 inside {[0:9]};
        indx2 inside {[0:9]};
        indx1 == (indx2 + 1);
        arr[indx1] == arr[indx2];
    }
endclass

initial begin
    cls obj = new;
    repeat(2) begin 
        obj.randomize;
        $display("indx1 = %d, ind2 = %d, arr = %p",
            obj.indx1,obj.indx2,obj.arr);
    end
end

endprogram

In reply to VC_45:

Probably, this should meet the query…
program tb;
class cls;
rand bit [3:0] arr[10], indx1, indx2;
constraint cons{
foreach(arr[i])
foreach(arr[j])

Please use code tags.

In reply to burra thanuja:

Can anyone plz help me with this constraint question…
Take a rand variable with array size 10,need to get unique values in each location without using unique keyword and for any of 2 locations we need to get same value?


class unq_arr;
 rand int unsigned d;
 rand int unsigned arr[10];

 constraint arr_c{
   foreach(arr[i]) { arr[i] inside {[1:10]};}
   foreach(arr[i]) { arr.sum() with (int'(item==arr[i])) == (arr[i]==d ? 2:1);}
 }

In reply to Alokpati:

Shouldn’t there be an additional constraint to ensure d’s value is within arr ?


constraint INSIDE { d inside { arr } ; } 

In reply to Have_A_Doubt:

In reply to Alokpati:
Shouldn’t there be an additional constraint to ensure d’s value is within arr ?


constraint INSIDE { d inside { arr } ; } 

It will work as solver should take care of it to satisfy the constraint.
Adding additional constraint on ‘d’ may help solver to solve constraint fast.


  arr[i]==d // it will force RHS to be equal to LHS, and 'd' doesn't have any constraint so it will work

In reply to Alokpati:

It will work as solver should take care of it to satisfy the constraint.

There is No constraint ( direct / indirect ) however to ensure this .

The constraint solver could solve the equation ( arr[i]==d ) to be 0 always thereby giving only Unique elements .

In reply to Have_A_Doubt:

In reply to Alokpati:
There is No constraint ( direct / indirect ) however to ensure this .
The constraint solver could solve the equation ( arr[i]==d ) to be 0 always thereby giving only Unique elements .

Agree… got your point. And strange my code didn’t fail when ran for loop of 100(check added in post_randomize to validate number of unique element).



class A;
  
  rand bit[4:0]  array[10];
  rand bit[4:0] num1,num2;
  
  constraint C1 { foreach(array[i]) foreach(array[j]) {if(i!=j) array[i]!= array[j];}}
    constraint C2 {num1 !=num2; num1<10;num2<10;}
    
    function void post_randomize();
                 $display("post_randomize: num1=%d, num2=%d",num1,num2);
                 array[num1]=array[num2];
                 
                 endfunction
    
    function print();
      foreach(array[i]) begin
        $display("arr[%d]=%d",i,array[i]);
        foreach(array[j]) 
          if(array[i]==array[j] && i!=j) $display("array[%d] and array[%d] matches",i,j);
    
      end
      			
                 endfunction
endclass


module tb;
  
  A a=new();
  initial begin
    assert(a.randomize());
    a.print();
  end
  
  
endmodule

In reply to burra thanuja:

Bellow is simple sollution i have written for the above problem

class my_rand;
  rand bit [5:0] a[10];
  rand int b,c;
  
  constraint sum_new {
    solve b,c before a;
    foreach(a[i]){
      if((i == b )|| (i==c) )
        (a.sum() with(int'((item == a[i]))) ) == 2;
      else
        (a.sum() with(int'((item == a[i]))) ) == 1;
    }
  }
  constraint n1{
    b inside {[0:9]};
    c inside {[0:9]};
    c != b;
    
  }
      
   
endclass
module mod ();
  initial
    begin
      my_rand me;
      me = new();
      me.randomize();
      $display("%p",me.a);
      
    end
  
endmodule
1 Like

You can get the address question with below code.
It’s working 100%.

Array = '{'h0, 'h1, 'h2, 'h2, 'h3, 'h6, 'h7, 'h8, 'hb, 'he}

class ABC;
rand bit [3:0] array[10]; 

    constraint a_c { 
                        foreach(array[i]) { 
                            if(i>0) { 
                                if(i == 3) 
                                    array[i] == array[i-1]; 
                                else 
                                    array[i] > array[i-1];
                            }

                        }
    } 
endclass                      
    
    module tb; 

        initial begin 
            ABC abc; 
                abc = new(); 
                abc.randomize(); 
          $display("Array = %p", abc.array);
              end 
    endmodule