# Constraint for two consecutive numbers be same

**URL:** <https://verificationacademy.com/forums/t/constraint-for-two-consecutive-numbers-be-same/39238>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [June 29, 2021, 12:04am UTC](https://verificationacademy.com/forums/t/constraint-for-two-consecutive-numbers-be-same/39238 "2021-06-29T00:04:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsivakumar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/e0b2c6/32.png) [@bsivakumar](https://verificationacademy.com/forums/u/bsivakumar)\
**Post date:** [June 29, 2021, 12:04am UTC](https://verificationacademy.com/forums/t/constraint-for-two-consecutive-numbers-be-same/39238/1 "2021-06-29T00:04:48Z")

</div>

Hi all,

How to write a constraint for two consecutive numbers are same.

Example pattern: array[15] ----\> {3,3,7,5,5,9,6,6,1,4,4,–}

I tried below code but its not working

```verilog
module conditional_con;
  class frame_t;
    rand int length[10];
    int k;
  
    constraint frame_sizes {
      
      foreach (length[i]){
        if(i!=0)
          i==i+2;
        else
          i==i;
        foreach(length[j]){
          if(j==i || j==i+1){
            if(j!=0)
            length[j]==length[j-1];
            else
              length[j]==length[j];}
          else{
            length[j]!=length[j-1];}}
           
          } 
            
    }
            
            
    
  endclass

  initial begin
     frame_t frame = new();
     integer i,j;
     for (j=0;j < 4; j++) begin
       $write("-------------------------------\n");
       $write("Randomize Value\n");
       i = frame.randomize();
        
       $display("length : %p",frame.length);
     end
     $write("-------------------------------\n");
  end

endmodule

```

Thanks,

---

<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:** [June 29, 2021, 12:37am UTC](https://verificationacademy.com/forums/t/constraint-for-two-consecutive-numbers-be-same/39238/2 "2021-06-29T00:37:11Z")

</div>

_In reply to [bsivakumar](https://verificationacademy.com/forums/t/constraint-for-two-consecutive-numbers-be-same/39238):_

Please use [code tags](https://verificationacademy.com/forums/announcements/please-format-your-code-code-and/code-tags) making your code easier to read. I have added them for you.

The requirements for your constraint is not very clear. Looking at your example, it appears you want the first elements to repeat, followed by a non repeating element. Then that pattern repeats. If that is the case, then the size of the array must be a multiple of 3.

```verilog
module conditional_con;
  class frame_t;
    rand bit [3:0] length[12];
    constraint frame_sizes {
      foreach (length[i])
        i%3==0->{
          length[i] == length[i+1];
          length[i+1] != length[i+2];
        }
    }
  endclass
  frame_t frame = new();
  initial begin
    repeat(4) begin
      $write("-------------------------------\n");
      $write("Randomize Value\n");
      assert(frame.randomize());
      $display("length : %0p",frame.length);
    end
    $write("-------------------------------\n");
  end
endmodule

```
