Randomly distribute ones in a n bit vector

I think Dave’s code works well for constraints. If you want to do it procedurally:

Compare the following code:
bit [n-1:0] pattern;

for(int i =0; i<N ; i++) // N= number of ones in array
begin
patern[i] =1’b1;
end
pattern.shuffle();