Generate Pattern 2, 33, 222, 5555, 22222, 777777 using constraints

I am trying write a constraint to generate this pattern 2, 33, 222, 5555, 22222, 777777, 2222222, 99999999, 2222222222 and so on
This is my constraint code

    rand longint a;
    int count=0;
    
    constraint value {

        //ODD
        if(count%2 == 1)
            a == odd(count);
        //EVEN
        else
            a == even(count);
    }
    

    function void post_randomize();
        count++;
    endfunction

    //ODD
    function longint odd(int x);
        longint num = 0;
        int value = x+2;
        for(int i=0;i <= x;i++) begin
            num = num*10+value;
        end
        return num;
    endfunction
    
    //EVEN
    function longint even(int x);
        longint num=0;
        for(int i=0;i <= x;i++) begin
            num = num*10+1;
        end
        return 2*num;
    endfunction

This is my output

#2

33

222

5555

22222

777777

2222222

99999999

222222222

12222222221

22222222222

1444444444443

2222222222222

166666666666665

222222222222222

The even pattern of 2s are coming properly.
But odd pattern is coming wrong

In reply to bachan21:

What output are you expecting after 99999999?

In reply to dave_59:

Yes.
Next output is 222222222 followed by 11111111111111111111 so on

In reply to bachan21:

Since this is not really a random pattern, you should be able to debug your function alone by single stepping or printing intermediate results.

In reply to bachan21:

i think we can use hexadecimal format and shift operations to print this pattern

  1. take the input patterns like “3”,“5”,“7”,“9”,“11”,“13” …
  2. convert it to hex decimal numbers
    longint value = str.atohex();
  3. using below logic we can get format(num = 0 )
    for (int i = 1; i <= x; i++)
    num = (num << no_of_bits) + value;
    Note: number of bits to shift → no of bits needed to represent a input number in hexadecimal
    if ( ip_value < 16 ) no_of_bits = 4;
    else if ( (ip_value < 256) && ( ip_value >= 16)) no_of_bits = 8;

Hope it helps

In reply to Desam:

Hi Desam,
can put this into code

In reply to bachan21:

Plz find the code in below link:

// Generate Pattern 2, 33, 222, 5555, 22222, 777777 using constraints
class c_1;
  rand bit [127:0] arr[];
  rand bit [127:0] arr2[];
  int unsigned arr3[10] = '{2, 3, 4, 5, 6, 7, 8, 9, 0, 1};
  rand int n;
  
  function new (string name = "c_1");  
  endfunction : new

  constraint a_1 {
    n == 22;
    arr.size() == n;
    arr2.size() == n;
    
    foreach(arr2[i]){
      if(i == 0){
        arr2[i] == 1;
      } else {
        arr2[i] == arr2[i - 1] * 10 + 1;
      }
    }
      
      foreach(arr[i]){
        if(i % 2 == 0){
          if(i == 0) {
            arr[i] == 2 ;
          } else {
            arr[i] == arr2[i] * 2;
          }
        } else {
          arr[i] == arr2[i] * arr3[(i % 10)];
        }
      }
  }
endclass : c_1        

    module tb;
      initial begin
        c_1 cc;
        cc = new();       
        cc.randomize();
        foreach(cc.arr[i])
          $display("cc.arr[%0d] = \t %d", i, cc.arr[i]);        
      end      
    endmodule : tb
//output
cc.arr[0] = 	                                       2
cc.arr[1] = 	                                      33
cc.arr[2] = 	                                     222
cc.arr[3] = 	                                    5555
cc.arr[4] = 	                                   22222
cc.arr[5] = 	                                  777777
cc.arr[6] = 	                                 2222222
cc.arr[7] = 	                                99999999
cc.arr[8] = 	                               222222222
cc.arr[9] = 	                              1111111111
1 Like
/*
2, 33, 222, 5555, 22222, 777777   --> a
1  2   3    4     5      6        --> count
*/
class pattern;
  int  count=1;
  rand bit [255:0] a;
  
  constraint c1 {
    if(count%2==1) a==print2(count);
    else           a==even(count);
  }
      
  function bit [255:0] print2(int c);
    bit [255:0] num=0;
    for (int i=0; i<c; i++) begin
      num = num*10+2;
    end
    return num;
  endfunction
    
  function bit [255:0] even(int c);
    bit [255:0] num=0;
    int shift = (c>9) ? 100 : 10;
    for (int i=0; i<c; i++) begin
      num = num*shift+(c+1);
    end
    return num;
  endfunction
    
    function void post_randomize();
      $display("%0d -> %0d", count, a);
      count++;
    endfunction
endclass

Output:
1 → 2
2 → 33
3 → 222
4 → 5555
. . . . . .
9 → 222222222
10 → 11111111111111111111
11 → 22222222222
12 → 131313131313131313131313

Hi @bachan21,

Pattern: 2, 33, 222, …, 9999_9999, 2_2222_2222, 11_1111_1111, 222_2222_2222, 3333_3333_3333, so on.

Code:

program automatic top;

  class MyClass;
    rand bit[255:0] num;
    bit[255:0] a[20];
    bit[255:0] cnt_array[20];
    bit[255:0] count = 0;
    
    function bit[255:0] even(input bit[255:0] value, input bit[255:0] add_factor);
      if(value == 0) return add_factor;
      even = add_factor;
      repeat(value) even = even * 10 + 2;
      return even;
    endfunction

    function bit[255:0] odd(input bit[255:0] value, input bit[255:0] add_factor);
      odd = add_factor;
      repeat(value) odd = odd * 10 + add_factor;
        return odd;
    endfunction
    
    function void post_randomize();
      a[count] = num;
      cnt_array[count] = count;
      count = count + 1;
    endfunction

   constraint c1 {
      if(count % 2 == 0) num == even(count, 2);
      else {
        if(count % 10 == 1) num == odd(count, 3);
        if(count % 10 == 3) num == odd(count, 5);
        if(count % 10 == 5) num == odd(count, 7);
        if(count % 10 == 7) num == odd(count, 9);
        if(count % 10 == 9) num == odd(count, 1);
      }
    }
  endclass

  initial begin
     MyClass m;
     m = new();
     repeat($size(m.a))
        if(!m.randomize()) $display("Randomization failed");
     foreach(m.a[i])
     $display("Count: %0d \t Value: %0d", m.cnt_array[i], m.a[i]);
  end
endprogram

Output:

# Count: 0 	 Value: 2
# Count: 1 	 Value: 33
# Count: 2 	 Value: 222
# Count: 3 	 Value: 5555
# Count: 4 	 Value: 22222
# Count: 5 	 Value: 777777
# Count: 6 	 Value: 2222222
# Count: 7 	 Value: 99999999
# Count: 8 	 Value: 222222222
# Count: 9 	 Value: 1111111111
# Count: 10 	 Value: 22222222222
# Count: 11 	 Value: 333333333333
# Count: 12 	 Value: 2222222222222
# Count: 13 	 Value: 55555555555555
# Count: 14 	 Value: 222222222222222
# Count: 15 	 Value: 7777777777777777
# Count: 16 	 Value: 22222222222222222
# Count: 17 	 Value: 999999999999999999
# Count: 18 	 Value: 2222222222222222222
# Count: 19 	 Value: 11111111111111111111

Thanks,
Naveen Kadian