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: