Constraint to print pattern 122333444455555

How to write constraint to print
1 22 333 4444 55555?
I tried to use foreach loop but can’t get the output.

1 Like
class cons_pattern;   
  rand int arr[5];
  
  constraint c1 {foreach(arr[i])
    arr[i]==pattern(i);	 			
  } 
    function int pattern(int i);
      static int num=1;
      int res;
              res=(i+1) *num;
              num=num+10**(i+1);
			return res;        
    endfunction    
endclass

It prints this output.
array : '{1, 22, 93, 244, 505}

//1 22 333 4444 55555

class trans;
  parameter num=5;
  bit [3:0] q[$];
  rand bit [3:0] k[num];

  function bit [3:0] varr1 (integer j);
    for(int i=0;i<j;i++) 
      q.push_back(j);
    varr1 = j;
  endfunction
  
  constraint cond{ 
    foreach(k[i]){      
      k[i] == varr1(i+1);
    } 
  }
      
  function void post_randomize();
    $display("array : %0p",q);
  endfunction : post_randomize
  
endclass : trans

trans tr;

module tb;
  initial begin
    tr = new();
    tr.randomize();
  end
endmodule : tb

i tried above code and it works using cadence and questa tool. Is there any other way to write its constraint and without writing any logic in post randomize function?

inside function change these 2 lines, it will work
res=(i+1) * num;
num=num+10**(i+1);

Calling a function in a constraint with side-effects is unpredictable because you can’t control how many times the function gets called in the evaluation of a constraint.

class first;
rand int ar[][];
  int a[$];
 function new();
	$display("Inside first");
 endfunction

constraint ct{
  ar.size()==6;
foreach(ar[i]){
  ar[i].size()==i+1;
}
  foreach(ar[i,j])
  {
    ar[i][j] == i+1;
  }
}

  function void post_randomize();
  foreach(ar[i,j])
    begin
      a.push_back(ar[i][j]);
    end
  endfunction
function dis();
  $display("ar=%p",a);
endfunction
endclass


module top;
initial begin
first f=new();
f.randomize();
f.dis();
end
endmodule

//output
Inside first
value ar='{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6}

1 Like

Thank you

class pattern;
	rand int a[];
	constraint c2 { a.size==6;}
	constraint c1 { foreach (a[i])
			a[i]==(1*(i+1)*(10**(i+1)-1))/9;}

endclass
module m;
pattern p;
initial
begin
	p=new;
	assert(p.randomize);
	foreach(p.a[i])
	$display("pattern is %d",p.a[i]);
		end
endmodule

thought this is simpler

class my_class;
  rand bit[3:0] my_q[$];
  rand bit[3:0] temp_val;

  constraint c1{
    temp_val inside {[1:9]};
  }

  function void pre_randomize();
    my_q.delete();
  endfunction

  function void post_randomize();
    for(int i=temp_val;i>0;i=i-1) begin
      repeat(i) begin
        my_q.push_front(i);
      end
    end
  endfunction
endclass

module top;
  my_class m1;

  initial begin
    m1=new();
    repeat(5) begin
      m1.randomize();
      $display("Value of temp_val=%0d",m1.temp_val);
      foreach(m1.my_q[i]) begin
        $write("%0d",m1.my_q[i]);
      end
      $display("\n=========");
    end
  end
endmodule

// Code your testbench here
// or browse Examples

class test;
  
  // pattern: 1223334444
  rand int unsigned value;
  
  int unsigned count;
  int unsigned curr_num = 1;
  int unsigned val_q[$];
  
  constraint cons{
    if(curr_num == 1) {
      value == 1;      
    }
      else if(count < curr_num) {
        value == curr_num;
      }       
  }
  
  function void post_randomize();
    count++;
    if(count == curr_num) begin
      curr_num++;
      count=0;
    end
    val_q.push_back(value);
    
  endfunction : post_randomize
endclass : test

module tb;
  
  initial begin
    test t = new();
    
    repeat(50) begin
      assert(t.randomize());  
    end
    
    $display(" %0p", t.val_q);
  end
  
endmodule : tb