# Constraint Interview Question

**URL:** <https://verificationacademy.com/forums/t/constraint-interview-question/44844>\
**Category:** SystemVerilog\
**Tags:** constraint-randomization\
**Created:** [January 5, 2024, 9:27pm UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844 "2024-01-05T21:27:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![totochan1985](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/f6c823/32.png) [@totochan1985](https://verificationacademy.com/forums/u/totochan1985)\
**Post date:** [January 5, 2024, 9:27pm UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/1 "2024-01-05T21:27:15Z")

</div>

Write a constraint where you have a 32 bit value bit [31:0] val where you’d want to randomize this to where every randomization would only allow 2 bits to differ from the previous randomization.

Can we solve it by using

` $countones(prev_val^cur_val) == 2)`

This is so because if only two bits are different in the previous and current value, their XOR should have only two bits set. Any other logic is welcome.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 6, 2024, 3:15am UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/2 "2024-01-06T03:15:56Z")

</div>

You should try it. [Asking better questions on the Verification Academy Forums with EDAPlayground - Verification Horizons](https://blogs.sw.siemens.com/verificationhorizons/2020/04/29/forums-with-edaplayground/)

---

<div class="post-metadata">

**Author:** ![totochan1985](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/f6c823/32.png) [@totochan1985](https://verificationacademy.com/forums/u/totochan1985)\
**Post date:** [January 6, 2024, 1:54pm UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/3 "2024-01-06T13:54:56Z")

</div>

This seems to work for me. Any other solutions/suggestions are welcome.

```auto
class abc;
 
  static bit [31:0] prev_val;
  rand bit [31:0] cur_val;
  
 static int count = 0;
 
constraint sel{
  
  (count > 0) -> ($countones((prev_val^cur_val))==2);   
    
  }
  
function void pre_randomize();
  if(count>0) begin
    prev_val = cur_val;
  end
    count+=1;
    
endfunction
    
endclass
    
   

module tb;
  initial begin
  abc xx1 = new();
    repeat(10) begin
    xx1.randomize();
      $display("prev_val = %b",xx1.prev_val);
      $display("cur_val = %b",xx1.cur_val);
      $display("count = %d diff = %d",xx1.count,$countones(xx1.cur_val^xx1.prev_val));
    end
    
  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 7, 2024, 2:53am UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/4 "2024-01-07T02:53:51Z")

</div>

No need for static variables or a counter.

This also works

```auto
class abc;
  rand bit [31:0] cur_val;
  constraint sel{
    $countones( const'(cur_val)^cur_val)==2;
  }
endclass

```

---

<div class="post-metadata">

**Author:** ![totochan1985](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/f6c823/32.png) [@totochan1985](https://verificationacademy.com/forums/u/totochan1985)\
**Post date:** [January 7, 2024, 4:58am UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/5 "2024-01-07T04:58:19Z")

</div>

Could you please explain the need for the “const`” in this solution. Where exactly are you comparing the previous randomized value with the current one?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 7, 2024, 10:09pm UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/6 "2024-01-07T22:09:23Z")

</div>

> [@Regarding const'() cast](https://verificationacademy.com/forums/t/regarding-const-cast/42359):
>
> Hi , I was trying to understand working of const’() cast. LRM 6.24.1 mentions An expression may be changed to a constant with a const cast. const'(x) Consider the following code : class C; rand bit[2:0] a ; function void pre\_randomize(); // 'a' could be assigned here endfunction constraint CONST { a == const'(a) + 1 ; } function void post\_randomize(); // 'a' could be assigned here endfunction endclass C c1; module test; initial begin c1 = new(); r…

---

<div class="post-metadata">

**Author:** ![dhserfer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@dhserfer](https://verificationacademy.com/forums/u/dhserfer)\
**Post date:** [January 23, 2024, 4:40am UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/8 "2024-01-23T04:40:37Z")

</div>

My apologies, I’m more than a little rusty at this, but does the constraint solver efficiently handle the ‘flip two bits’ constraint via $countones? (A long time ago) I would have generated two values between 0 and 31 and inverted those bits.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 23, 2024, 5:25am UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/9 "2024-01-23T05:25:09Z")

</div>

That might be fine if there were no other constraints on the random value. But what if there was another constraint like `cur_val % 5 == 0` in addition to the number of changed bits? Generating the equations for that would be much less efficient.

---

<div class="post-metadata">

**Author:** ![lranjitha](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/85e7bf/32.png) [@lranjitha](https://verificationacademy.com/forums/u/lranjitha)\
**Post date:** [July 11, 2025, 6:12am UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/12 "2025-07-11T06:12:50Z")

</div>

Hi Dave,

I was trying this code and ended up with below error

Error-[SE] Syntax error  
Following verilog source has syntax error :  
“testbench.sv”, 458: token is ‘const’  
$countones( const’(cur\_val)^cur\_val)==2;  
^  
SystemVerilog keyword ‘const’ is not expected to be used in this context.

could you help on this?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [July 11, 2025, 4:00pm UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/13 "2025-07-11T16:00:43Z")

</div>

> [@Pattern Generation](https://verificationacademy.com/forums/t/pattern-generation/34173/8):
>
> In reply to [Rohi\_417](https://verificationacademy.com/forums/t/pattern-generation/34173/7): It looks like your tool has not implemented a const cast. Maybe you have an older version. Please contact your vendor for support.

---

<div class="post-metadata">

**Author:** ![Manoj\_Y](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@Manoj\_Y](https://verificationacademy.com/forums/u/Manoj_Y)\
**Post date:** [September 17, 2025, 12:58am UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/14 "2025-09-17T00:58:01Z")

</div>

I tried this way, and it worked for me. crude, but works if we cant remember solution using XOR!!! Thanks.

class class1;  
rand bit [31:0] a;  
static bit [31:0] prev\_a;  
static int cnt;

//make sure number of 1’s are always 2.  
constraint c1{  
$countones(a) == 2;  
}  
//constraint the same location to be not 1’b1.  
constraint c2{  
if(cnt \>0)  
{  
foreach(prev\_a[i]) {  
if(prev\_a[i] ==1){  
a[i] != 1;  
}  
}

```auto
}

```

}

function post\_randomize();  
cnt = cnt+1;  
prev\_a = a;

endfunction

endclass

---

<div class="post-metadata">

**Author:** ![Have\_A\_Doubt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Have\_A\_Doubt](https://verificationacademy.com/forums/u/Have_A_Doubt)\
**Post date:** [September 18, 2025, 5:54pm UTC](https://verificationacademy.com/forums/t/constraint-interview-question/44844/15 "2025-09-18T17:54:00Z")

</div>

An alternate solution without using $countones

```verilog
class class1;
  rand bit [9:0] a;
  rand int ind1 , ind2 ;

  // Constraint the Indexes which would be 1’b1.
  constraint c1{ if( const'(a) != 0 ) // False for 1st call to randomize
                  { !(ind1 inside { const'(ind1) , const'(ind2) } );
                    !(ind2 inside { const'(ind1) , const'(ind2) } ); 
                  }
                
                  ind1 != ind2 ;
                
                  ind1 inside { [0:9] }; // Alt: [0:($bits(a)-1)]
                  ind2 inside { [0:9] }; // Alt: [0:($bits(a)-1)]
               }  

 // Make sure number of 1’s are always 2.  
  constraint c2{ foreach(a[i])
                 { if(i inside { ind1 , ind2 } )
                    a[i] == 1;
                   else
                    a[i] == 0;                     
                 }
               }
endclass

```
