# Constraint Randomization Interview Question

**URL:** <https://verificationacademy.com/forums/t/constraint-randomization-interview-question/39874>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [December 8, 2021, 4:42am UTC](https://verificationacademy.com/forums/t/constraint-randomization-interview-question/39874 "2021-12-08T04:42:51Z")\
**Posts on this page:** 1\
**Showing post:** 10

<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:** [December 10, 2021, 12:00pm UTC](https://verificationacademy.com/forums/t/constraint-randomization-interview-question/39874/10 "2021-12-10T12:00:54Z")

</div>

I tried an alternate solution ::

```verilog

class c; 
 
  rand bit[31:0] v;  
 
  rand int int1 ;
 
  constraint LOW_BOUND { int1 inside { [0 : 22] } ; }
 
  constraint ct22 { $countones(v) == ( 32 - 10 ) ; }  
 
  constraint VAL { foreach( v[i] ) // Iterates from 31 to 0 .
                        {   
                             if( i == int1 ) 
                         { v[i+:10] == 10'h0 ; } // Issue here , not sure why      
                        }
                   }
 
endclass 
 
module m; 
    c c1=new(); 
 
  initial begin 
    repeat(5) begin
      if (! c1.randomize() ) $display(" Randomization fails ");
      $display("c1.v= %b", c1.v);
 
    end
  end
endmodule  

// When Max( int1 ) is 22 , v[22 +: 10] == 10'h0 i.e v[31:22] == 10'h0 ;

// When Min( int1 ) is 0 , v[0 +: 10] == 10'h0 i.e v[9:0] == 10'h0 ;

```

This throws Randomization Failure on some Simulators while it works on others .

Any suggestions ?

---

_[View the full topic](https://verificationacademy.com/forums/t/constraint-randomization-interview-question/39874)._
