Interview question on constraint

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?

In reply to burra thanuja:

module abc;
  class aa;
    rand int unsigned val[10];
    
    constraint s1 {
      foreach(val[i]){
        val[i] < 10;
        foreach(val[j]){
          if(i != j)
            val[i] != val[j];
        }
      }
    }	
  endclass
          
  initial begin
    aa a1 = new();
    a1.randomize();
    $display(a1.val);
  end
endmodule

this is the way to generate the unique values with out using unique keyword

In reply to yourcheers:

thank you for ur reply. sir i too know how to generate the unique value but my question is not only to generate unique values but also for any of 2 locations we need to get same value?
if possible plz go through the question once and plz help me with it if you aware of it.
thank you in advance

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).