class my_array;
rand bit[4:0] array[4][4];
int i,j;
rand int n;
//This is first constraint limiting value of each item
constraint array_item_val {
foreach (array[i]) { // for each row
foreach (array[,j]) { // for each column in array
array[i][j] inside {[0:1]}; // each element is either 0/1
}
}
}
constraint array_col_sum {
foreach (array[i,j]) {
array.sum() with (item.index(2) == j ? int'(item) : 0) == 1; // col max sum is 1
}
}
function void display();
foreach (array[i]) begin
foreach (array[i][j]) begin
$write(" %d ",array[i][j]);
end
$display("\n");
end
endfunction
endclass
module my_array_randomization;
my_array ma;
initial begin
ma = new();
ma.randomize();
ma.display();
end
endmodule
this should generate sum of elements in column to 1.