Tricky array value randomization

Hi all,

I have an array value randomization related question. Lets say we have an two arrays.


int a [4] = {0,1,2,3};
int b [8];

Now I want to generate all elements of b such it contains all elements of a in some random order but not unique. The following piece of code does not solve problem. The constraint solver could generate b as following.


int b [8]= {0,1,2,2,1,1,0,0};

I want b to have all elements of a but in some random repeated way.
Is there some way we can achieve this ?


 constraint another_array_c {
  foreach (b[i])
    b[i] inside { a };
  }

Thank you,


module vf_acad;

class c;

         int a[4] = {0, 1, 2, 3};
    rand int b[8];

    constraint c_arr {
        foreach (b[i]) {
            if (i < 4) {
                b[i] == a[i];
            } else {
                b[i] inside { a };
            }
        }
    }

    function void post_randomize();
        b.shuffle();
    endfunction : post_randomize

    function void display();
        $display("a=%0p", a);
        $display("b=%0p", b);
    endfunction : display

endclass : c

initial begin

    c obj1;
    obj1 = new();
    repeat(10) begin
        obj1.randomize();
        obj1.display();
    end
end

endmodule : vf_acad

In reply to suhasp:

Also add:


foreach (a[i])
  a[i] inside { b };

This will ensure that all elements of ‘a’ are inside ‘b’. Otherwise, it would be possible to randomize a value for ‘b’ that, for example, doesn’t contain ‘a[0]’.

Thank you for the help, Trying out solutions I will post final solution that works.