Given an integer n, create an array such that each value is repeated twice.
If this is the only constraint, the following example code using post_randomize() would work.
class constraint_test;
rand int arr[];
int n = 4;
constraint c7 {arr.size() == n; }//n*2;
function void post_randomize();
super.post_randomize();
begin
int tmp_q[$];
foreach(this.arr[i])
repeat(2) tmp_q.push_back(this.arr[i]);
this.arr.delete();
this.arr = tmp_q;
end
endfunction
endclass
Note that, each integer number is random, not sequential number like your showed as example.
For example;
n=3->[1,1,2,2,3,3]
n=4->[1,1,2,2,3,3,4,4]
If you are expecting like these, you don’t need to use randomize().
I don’t see any need for classes, randomization or sum(). Is it required to use those elements? How about this solution:
// Given an integer n, create an array such that each value is repeated twice.
// This array is a solution for all n: '{1, 1}
// But let's add a few more constraints:
// 1. the array has 2*n entries
// 2. items in the array are between 1 and n
module tb;
parameter N = 4;
int b[N*2];
initial begin
foreach (b[i])
b[i] = (i % N) + 1;
foreach (b[i])
$display("%0d: %0d", i, b[i]);
end
endmodule