# Constraint Solver

**URL:** <https://verificationacademy.com/forums/t/constraint-solver/32810>\
**Category:** SystemVerilog\
**Tags:** SV-Constraint-Solver, SystemVerilog\
**Created:** [February 6, 2017, 10:15am UTC](https://verificationacademy.com/forums/t/constraint-solver/32810 "2017-02-06T10:15:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![soumyarayaprolu](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/a698b9/32.png) [@soumyarayaprolu](https://verificationacademy.com/forums/u/soumyarayaprolu)\
**Post date:** [February 6, 2017, 10:15am UTC](https://verificationacademy.com/forums/t/constraint-solver/32810/1 "2017-02-06T10:15:04Z")

</div>

Hi,

I have below snippet of code:

```verilog

constraint c1 {
   solve mode before depth;
   if(mode == 0) depth inside {1, 2};
   else if(mode == 1) depth inside {3, 4};
   else depth >0; depth <4;
}

constraint c2 {
    mode == 0;
}

```

wherein mode can take values 0 or 1 and depth can be anything

Now if I do .randomize for above class, it states Randomization Error with two conflicting constraints c1 and c2 mentioned above. Am I missing anything here? Can u please help

---

<div class="post-metadata">

**Author:** ![shanya](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shanya](https://verificationacademy.com/forums/u/shanya)\
**Post date:** [February 6, 2017, 12:22pm UTC](https://verificationacademy.com/forums/t/constraint-solver/32810/2 "2017-02-06T12:22:50Z")

</div>

_In reply to [soumyarayaprolu](https://verificationacademy.com/forums/t/constraint-solver/32810):_

```verilog

class test_class;
  rand bit mode;
  rand bit [2:0] depth;

  constraint c1 {
               solve mode before depth;
               if(mode == 0) depth inside {1, 2};
                else if(mode == 1) depth inside {3, 4};
                 else depth >0; depth <4;
              }

  constraint c2 {
               mode == 0;
              }

endclass

module test;
  test_class t1;

  initial begin
    t1 = new;
    repeat(5) begin
      t1.randomize;
      $display(" value of mode is ",t1.mode);
      $display(" value of depth is ",t1.depth);
    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:** [February 6, 2017, 3:23pm UTC](https://verificationacademy.com/forums/t/constraint-solver/32810/3 "2017-02-06T15:23:43Z")

</div>

_In reply to [soumyarayaprolu](https://verificationacademy.com/forums/t/constraint-solver/32810/2):_

Your code worked for me.
